我有这个 df
df<-data.frame(category=c("food","clothes","clothes","food"),item=c("bread","shirt","shoes","fish"),number=c(2,5,8,3))
并且我需要对更大的“食物”和“衣服”集合中的物品的价值求和,以获得如下所示的表格。
tot<-data.frame(category=c("food","clothes"),number=c(5,13))
我该怎么做?
谢谢
如果您不介意使用 package dplyr
:
library(dplyr)
tot <- df %>% group_by(category) %>% summarise(number = sum(number))
输出:
> tot
# A tibble: 2 x 2
category number
<chr> <dbl>
1 clothes 13
2 food 5
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句