I have two df and would like to bind the value from df1 with the value from df2. dfs have same variables and rows. How should I do it?
df1<-structure(list(c1 = 750, c2 = 21, c3 = 65), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))
df2<- structure(list(c1 = "Student Enrollment", c2 = "Faculty Number",
c3 = "Graduated"), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"))
CodePudding user response:
For data frames that have more than one row:
# create two-row data
df1 <- rbind(df1, df1)
df2 <- rbind(df2, df2)
as_tibble(mapply(paste, df2, df1, SIMPLIFY=F))
CodePudding user response:
Use paste
:
d <- paste(df2, df1)
names(d) <- names(df1)
c1 c2 c3
"Student Enrollment 750" "Faculty Number 21" "Graduated 65"
CodePudding user response:
Using across
library(dplyr)
library(stringr)
df2 %>%
mutate(across(everything(), ~ str_c(., ' ', df1[[cur_column()]])))
-output
# A tibble: 1 × 3
c1 c2 c3
<chr> <chr> <chr>
1 Student Enrollment 750 Faculty Number 21 Graduated 65