Combine dfs by common column importing selected columns in R

烈酒焚心 提交于 2021-02-08 05:31:37

问题


I would like to merge data.frames by common "names" column but only selecting the "PA" columns.

df <- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), `S1`=c(1,2,2,0,1), `S2`=c(2,50,40,30,22), `S3`=c( 0,100,135,256,303), `S4`=c(0,10,17,73,74),check.names=FALSE)
df2<- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), AB=c(0,30,30,40,2), PA=c(2,4,5,6,7))
df3<- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), AB=c(100,300,300,400,200), PA=c(3,5,7,8,7))
df4<- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), AB=c(110,310,310,410,210), PA=c(30,50,70,80,70))

At the moment, this is importing all the columns, but I would like to select only to import the "PA"

library(dplyr)

dff<- list(df,df2,df3, df4) %>%
        Reduce(function(df,df2) left_join(df,df2,by="names"), .)

Curref dff:

  names S1   S2  S3   S4   AB.x PA.x  AB.y  PA.y AB  PA 
1 Obs1  1    2   0    0    0    2     100   3    110 30
2 Obs2  2    50  100  10   30   4     300   5    310 50 
3 Obs3  2    40  135  17   30   5     300   7    310 70 
4 Obs4  0    30  256  73   40   6     400   8    410 80 
5 Obs5  1    22  303  74   2    7     200   7    210 70

Desired:

  names S1   S2  S3   S4    PA_df2   PA_df3 PA_df4 
1 Obs1  1    2   0    0     2        3      30
2 Obs2  2    50  100  10    4        5      50 
3 Obs3  2    40  135  17    5        7      70 
4 Obs4  0    30  256  73    6        8      80 
5 Obs5  1    22  303  74    7         7     70

回答1:


Using SQL like joins, does this work:

library(dplyr)
df %>% inner_join(df2 %>% select(names, 'PA_df2' = PA)) %>% 
          inner_join(df3 %>% select(names, 'PA_df3' = PA)) %>% 
          inner_join(df4 %>% select(names, 'PA_df4' = PA))
Joining, by = "names"
Joining, by = "names"
Joining, by = "names"
  names S1 S2  S3 S4 PA_df2 PA_df3 PA_df4
1  Obs1  1  2   0  0      2      3     30
2  Obs2  2 50 100 10      4      5     50
3  Obs3  2 40 135 17      5      7     70
4  Obs4  0 30 256 73      6      8     80
5  Obs5  1 22 303 74      7      7     70


来源:https://stackoverflow.com/questions/65021022/combine-dfs-by-common-column-importing-selected-columns-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!