简体   繁体   中英

R- Populating a dataframe based on another one with conditions

im new on R and i have a data set of 22x252, the 252 have many repeated values on column 1(ID). I made another dataset that has nrows of the unique values (with those values already populated), and i want to populate the rest of the columns based on the other dataset (basically summing all the values that share the same value in column 1.)

Is there a basic function that enables me to do this?

Thanks & Regards

We can use aggregate in base R . Assuming the column name of first column is 'ID' and all other columns are numeric class, we group by 'ID' and get the sum of the rest of the columns in aggregate

aggregate(.~ ID, df1, sum, na.rm = TRUE)

Or with dplyr

library(dplyr)
df1 %>%
  group_by(ID) %>%
  summarise_at(vars(-group_cols()), sum, na.rm = TRUE)

Or with new version with across

df1 %>%
  group_by(ID) %>%
  summarise(across(-group_cols(), sum, na.rm = TRUE))

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