Home > Enterprise >  Merge dataframes ans sum by name in R
Merge dataframes ans sum by name in R

Time:05-23

I have the following dataframes :

df1
     V1  V2
 G18941  17
 G20092 534
 G19692  10
 G19703 260
 G16777 231
 G20045   0
 ...

and

df2
   V1  V2
 G18941   0
 G20092 179
 G19692   3
 G19703 174
 G16777 147
 G20045 111
 ...

and I would have this one :

df3
    V1  V2
1 G18941  17
2 G20092 713
3 G19692  13
4 G19703 434
5 G16777 378
6 G20045 111

I have tried this piece of code that I found

df3 <- ddply(merge(df1, df2, all.x=TRUE), 
              .(V1,V2), summarise, V2=sum(as.numeric(V2)))

But it creates a df3 exactly like the df2...

I have also tried this code from Merge data frames and sum columns with the same name :

df3 <- rbindlist(list(df1, df2))[, lapply(.SD, sum), by = V2]

But I get the following error:

Error in gsum(V1) : sum is not meaningful for factors.

If you know how to do it I'm interested.Thanks.

CodePudding user response:

library(dplyr)
df1 %>% rbind(df2) %>% 
  group_by(V1) %>% 
  summarise(V2 = sum(V2)) %>% 
  arrange(V2)

Output:

# A tibble: 6 x 2
  V1        V2
  <chr>  <int>
1 G19692    13
2 G18941    17
3 G20045   111
4 G16777   378
5 G19703   434
6 G20092   713

CodePudding user response:

Ok, the following code works very well :

library(dplyr)
df3 <- bind_rows(df1, df2) %>%
  group_by(V1) %>%
  summarise_all(., sum, na.rm = TRUE)
  • Related