简体   繁体   中英

R : Getting the sum of columns in a data.frame group by a certain column

I have a sample data.frame as below, I want to create another data.frame that contains the statistical information of that table by a certain column, how can I do that?

Like for example in the data.frame below, I like to get the sum of each column by Chart.

Sample data.frame:

Chart    Sum     Sum_Squares    Count     Average
Chart1   2           4            4         1
Chart1   3           9            3         1.5
Chart2   4           16           5         2
Chart2   5           25           2         2.5

Desired output:

Chart    Sum_sum      Sum_square_sum      Count_sum      Average_sum
Chart1      5              13                 7              2.5
Chart2      9              41                 7              4.5

I have tried below code but the return table only contains Chart and V1. sum_stat is the data.frame

  sum_stat = data.table(spc_point[,c("CHART", "SUM", "SUM_SQUARES", "COUNT", "AVERAGE")])[,c(SUM_SUM=sum(SUM), SUM_SQUARE_SUM=sum(SUM_SQUARES), COUNT_SUM=sum(COUNT), AVERAGE_SUM=sum(AVERAGE)),by=list(CHART)]

Thanks ahead

I'm going to advocate using data.table. try this:

data<-data.table("Chart"=c("Chart1","Chart1","Chart2","Chart2"), "Sum"=c(2,3,4,5),"Sum_Squares"=c(4,9,16,25),"Count"=c(4,3,5,2),"Average"=c(1,1.5,2,2.5),key="Chart")

and then simply:

summed.data<-data[,lapply(.SD,sum),by=Chart]

find data.table package, read vignette and faq - use it :)

You may consider dplyr . Suppose df is your data frame, the following will produce the desired result.

library(dplyr)
df %.% group_by(Chart) %.% 
    summarise(Sum=sum(Sum), 
              Sum_Squares = sum(Sum_Squares), 
              Count= sum(Count),
              Average= sum(Average))

or it can be laid out like that in data.table too :

dt = as.data.table(df)
dt[, list(Sum=sum(Sum), 
          Sum_Squares = sum(Sum_Squares), 
          Count= sum(Count),
          Average= sum(Average)),
   by=Chart]

In base R:

aggregate(df[,2:5],by=list(df$Chart),FUN=sum)
#   Group.1 Sum Sum_Squares Count Average
# 1  Chart1   5          13     7     2.5
# 2  Chart2   9          41     7     4.5

As @AnandaMahto points out, the formula syntax for aggregate(...) is simpler and cleaner.

aggregate(. ~ Chart, df, sum)
#    Chart Sum Sum_Squares Count Average
# 1 Chart1   5          13     7     2.5
# 2 Chart2   9          41     7     4.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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