简体   繁体   English

聚合数据框R的2列以上

[英]Aggregating over 2 columns of a dataframe R

My dataframe is as follows 我的数据框如下

TreeID    Species    PlotNo    Basalarea
12345       A          1         120
13242       B          7         310
14567       D          8         250
13245       B          1         305
13426       B          1         307
13289       A          3         118

I used 我用了

newdata<- aggregate(Basalarea~PlotNo+Species, data, sum, na.rm=TRUE)

to aggregate all the values such that 汇总所有值,以便

 newdata
     Species    PlotNo    Basalarea
       A          1         120
       A          3         118
       B          1         some value
       B          7         310
       D          8         250

This is great but I would like a dataframe such that 这很棒,但是我想要一个这样的数据框

PlotNo    A       B            D
 1        120    some value    0
 3        118    0             0
 7        0      310           0
 8        0      0            250

How do I obtain the above dataframe? 如何获得以上数据框?

We can use dcast to convert from long to wide format. 我们可以使用dcast将长格式转换为宽格式。 Specify the fun.aggregate as sum . fun.aggregate指定为sum

library(reshape2)
dcast(df1, PlotNo~Species, value.var='Basalarea', sum)
#  PlotNo   A   B   D
#1      1 120 612   0
#2      3 118   0   0
#3      7   0 310   0
#4      8   0   0 250

Or a base R option would be using xtabs . 否则, base R选项将使用xtabs By default it gets the sum of the 'Basalarea' for the combinations of 'PlotNo' and 'Species'. 默认情况下,对于“ PlotNo”和“ Species”的组合,它获取“ Basalarea”的sum

xtabs(Basalarea~PlotNo+Species, df1)
#     Species
#PlotNo   A   B   D
#     1 120 612   0
#     3 118   0   0
#     7   0 310   0
#     8   0   0 250

Or another base R option is tapply tapply另一个base R选项

with(df1, tapply(Basalarea, list(PlotNo, Species), FUN=sum))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM