Reputation: 2301
I have two data.frame, one for data (df), one for var label (vl).
How can I join them together and get sth looks like following:
Sample data df and vl can be build using codes:
df<-structure(list(ID = c(1, 1, 1, 2, 2, 3, 3, 3), Date = c("Day 1",
"Day 7", "Day 29", "Day_8", "Day9", "Day7", "Day.1", "Day 21"
), Score = c("A", "B", "E", "D", "F", "G", "A", "B"), Pass = c("Y",
"Y", "N", "Y", "N", "N", "Y", "Y")), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"))
vl <-structure(list(Var = c("ID", "Score", "Pass", "Date"), Label = c("Subject ID",
"Exam Score", "Pass or Fail the Exam", "Exam Date")), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 0
Views: 42
Reputation: 388962
In R, dataframes cannot have double column names so one way would to shift your original column names as first row.
setNames(rbind(names(df), df), vl$Label[match(names(df), vl$Var)])
# `Subject ID` `Exam Date` `Exam Score` `Pass or Fail the Exam`
# <chr> <chr> <chr> <chr>
#1 ID Date Score Pass
#2 1 Day 1 A Y
#3 1 Day 7 B Y
#4 1 Day 29 E N
#5 2 Day_8 D Y
#6 2 Day9 F N
#7 3 Day7 G N
#8 3 Day.1 A Y
#9 3 Day 21 B Y
Upvotes: 1