Mark
Mark

Reputation: 1769

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.

Upvotes: 1

Views: 137

Answers (1)

akrun
akrun

Reputation: 887241

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

Upvotes: 1

Related Questions