I have a table of data that has a unique ID in the first column and then the next 5 columns have data. The first column has some rows that have the same unique ID. I want to sum add all of the rows with the same unique ID so my output only has one row for each of those unique IDs. I have seen some methods to do this over just one other column but I need it over 5 other columns.
Asked
Active
Viewed 401 times
-1
-
Did you look at this? https://stackoverflow.com/questions/9723208/aggregate-summarize-multiple-variables-per-group-e-g-sum-mean It shows how to do it for multiple columns. – Ronak Shah Mar 31 '20 at 12:44
1 Answers
1
You might want to use dplyr to work with dataframe. Install it if you do not have.
install.packages("dplyr")
Assuming your dataframe is df
with ID
column and other five columns are numeric, this will sum all those 5 cols by ID.
library(dplyr)
df %>%
group_by(ID) %>%
summarise_all(sum)

nurandi
- 1,588
- 1
- 11
- 20