2017-08-15 180 views
2

我在v提供兩種dataframes匹配列的值如何總結,同時合併兩個dataframes r中

ship_no bay_1 bay_2 bay_3 bay_5  bay_6 
ABC  0  10  15  20  30 
DEF  10  20  0  25  10 
ERT  0  10  0  10   0 


ship_no bay_1 bay_2 bay_7 bay_5  bay_6 
ABC  10  10  10  0   0 
DEF  10  10  0  15  10 
ERT  0  0  0  10  0 

我要添加列值,而合併上述兩個dataframes在列鍵ship_no

我的期望dataframe將是

ship_no bay_1 bay_2 bay_3 bay_5  bay_6  bay_7 
    ABC  10  20  15  20  30  10 
    DEF  20  30  0  40  20  0 
    ERT  0  10  0  20  0   0 

我該怎麼做r?

回答

2

我們可以將數據集在list,使用rbindlist到rbind數據集,通過 'ship_no' 分組,讓其他列

library(data.table) 
rbindlist(list(df1, df2), fill = TRUE)[,lapply(.SD, sum, na.rm = TRUE) , ship_no] 
# ship_no bay_1 bay_2 bay_3 bay_5 bay_6 bay_7 
#1:  ABC 10 20 15 20 30 10 
#2:  DEF 20 30  0 40 20  0 
#3:  ERT  0 10  0 20  0  0 

sum另一種辦法是dplyr

library(dplyr) 
bind_rows(df1, df2) %>% 
      group_by(ship_no) %>% 
      summarise_all(funs(sum(., na.rm = TRUE))) 
# A tibble: 3 x 7 
# ship_no bay_1 bay_2 bay_3 bay_5 bay_6 bay_7 
# <chr> <int> <int> <int> <int> <int> <int> 
#1  ABC 10 20 15 20 30 10 
#2  DEF 20 30  0 40 20  0 
#3  ERT  0 10  0 20  0  0