简体   繁体   中英

How to group by rows when one column has the same value

I have ended up with a dataframe called total that looks like this:

Rpt         Col1    Col2   Col3
Jim          5        0      4
Jim          0        8      8
Charlie      6        7      4
Jim          6        2      4
Charlie      5        5      0

I would like to merge the rows with the same name in the Rpt column so that it looks like:

  Rpt         Col1    Col2   Col3
  Jim          11      10     16
  Charlie      11      12      4

I tried using dplyr but I probably dont really understand the syntax properly so I tried

library(dplyr)
total2<- total %>% count(Rpt)

but it gives me a single count column which doesnt really seem to count anything I want

With dplyr you can use summarise_each to compute the sum per Rpt in each column:

library(dplyr)
total %>% 
  group_by(Rpt) %>%
  summarise_each(funs(sum))

Or using data.table (slightly different approach than that by Pgibas):

library(data.table)
setDT(total)[, lapply(.SD, sum), by = Rpt]

Note that dplyr's count function is used to count the number of rows per group in a data frame.

Try:

aggregate(. ~ Rpt, data = df, FUN = sum)
      Rpt Col1 Col2 Col3
1 Charlie   11   12    4
2     Jim   11   10   16

Example using data.table

library(data.table)

total <- data.table(total)
total[, list(sum(Col1),sum(Col2),sum(Col3)), by=list(Rpt)]

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