我有一个如下所示的数据框:
df1 = structure(list(ID = structure(c(1L, 1L, 2L, 2L), .Label = c("A1",
"A2"), class = "factor"), DAY = structure(c(1392647220, 1392733620,
1392647220, 1392733620), class = c("POSIXct", "POSIXt"), tzone = ""),
P = c(10, 5, 100, 80)), .Names = c("ID", "DAY", "P"), row.names = c(NA,
-4L), class = "data.frame")
但有50k观察值
我想把它折叠成这样:
df2 <- data.frame(ID = c("A1","A2"), TotalP=c(15,180))
在R中这样做的最佳方式是什么?
谢谢!
答案 0 :(得分:2)
使用aggregate
:
> aggregate(P ~ ID, data=df1, FUN=sum)
ID P
1 A1 15
2 A2 180
答案 1 :(得分:1)
尝试aggregate
> aggregate(df1$P, list(ID = df1$ID), sum)
## ID x
## 1 A1 15
## 2 A2 180
还有by
。我们可以将其与stack
> rev(stack(by(df1$P, df1$ID, sum)))
## ind values
## 1 A1 15
## 2 A2 180
并且始终是流行的长篇版本
> W <- lapply(split(df1$P, df1$ID), sum)
> data.frame(ID = names(W), TotalP = unlist(W, use.names = FALSE))
## ID TotalP
## 1 A1 15
## 2 A2 180