简体   繁体   中英

Create dataframe using some columns of other dataframes

I have two dataframes

x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John", "Dora"))
y <- data.frame("AA" = c(11,19), "Re" = 11:12)

I would like to create a third dataframe whose columns are SN and Name from x , and AA from y . But with

df=cbind(x$SN,x$Name,y$AA)

I obtain a wrong result.

Using select and bind_cols from dplyr

library(dplyr)
x %>% 
  select(SN, Age) %>% 
  bind_cols(y %>% 
              select(AA))
#  SN Age AA
#1  1  21 11
#2  2  15 19

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