r - Average Value in other cells for Unique Groups -
i have following sample dataset. first 3 columns identify specific group.
dat <- read.table(header=true, text=" id lfrom lto it1 it2 it3 it4 1120 1 2 47 152 259 140 2000 1.1 2.1 88 236 251 145 2000 1.2 2.1 72 263 331 147 1120 1 2 71 207 290 242 1120 1 2 47 152 259 140 2001 1.2 2.1 72 263 331 147 2001 1.1 2 71 207 290 242 1120 1 2 47 152 259 140 2000 1.1 2.1 88 236 251 145 1120 1 2 72 263 331 147 2000 1.1 2.1 71 207 290 242 ") dat id lfrom lto it1 it2 it3 it4 1 1120 1.0 2.0 47 152 259 140 2 2000 1.1 2.1 88 236 251 145 3 2000 1.2 2.1 72 263 331 147 4 1120 1.0 2.0 71 207 290 242 5 1120 1.0 2.0 47 152 259 140 6 2001 1.2 2.1 72 263 331 147 7 2001 1.1 2.0 71 207 290 242 8 1120 1.0 2.0 47 152 259 140 9 2000 1.1 2.1 88 236 251 145 10 1120 1.0 2.0 72 263 331 147 11 2000 1.1 2.1 71 207 290 242
by using duplicated
function can find out unique groups.
dat[!duplicated(dat[,1:3]),] id lfrom lto it1 it2 it3 it4 1 1120 1.0 2.0 47 152 259 140 2 2000 1.1 2.1 88 236 251 145 3 2000 1.2 2.1 72 263 331 147 6 2001 1.2 2.1 72 263 331 147 7 2001 1.1 2.0 71 207 290 242
the frequency of unique groups.
aggregate(data = transform(dat[1:3], freq = seq_len(nrow(dat[1:3]))), freq ~ ., length) id lfrom lto freq 1 1120 1.0 2.0 5 2 2001 1.1 2.0 1 3 2000 1.1 2.1 3 4 2000 1.2 2.1 1 5 2001 1.2 2.1 1
i need results following (average value of last 4 columns common first 3 columns) using plyr
ordplyr
packages. appreciated.
id lfrom lto it1 it2 it3 it4 1 1120 1.0 2.0 67.5 213.1 285.2 163.5 2 2000 1.1 2.1 69.9 218.6 288.3 173.7 3 2000 1.2 2.1 72.0 263.0 331.0 147.0 4 2001 1.2 2.1 72.0 263.0 331.0 147.0 5 2001 1.1 2.0 71.0 207.0 290.0 242.0
using data.table
:
library(data.table) setdt(dat)[, lapply(.sd, mean), by=.(id, lfrom, lto)]
Comments
Post a Comment