挖掘不可能被认领的骷髅,为什么不使用aggregate()
?
dat = read.table(header = TRUE, sep = ";", text = "D1 ;hurs
1 ;0.12
1 ;0.23
1 ;0.34
1 ;0.01
2 ;0.24
2 ;0.67
2 ;0.78
2 ;0.98")
aggregate(hurs ~ D1, dat, c)
# D1 hurs.1 hurs.2 hurs.3 hurs.4
# 1 1 0.12 0.23 0.34 0.01
# 2 2 0.24 0.67 0.78 0.98
如果 D1 中每个 id 的长度不一样,也可以reshape()
先创建“时间”变量后使用 base R:
dat2 <- dat[-8, ]
dat2$timeSeq <- ave(dat2$D1, dat2$D1, FUN = seq_along)
reshape(dat2, direction="wide", idvar="D1", timevar="timeSeq")
# D1 hurs.1 hurs.2 hurs.3 hurs.4
# 1 1 0.12 0.23 0.34 0.01
# 5 2 0.24 0.67 0.78 NA