I currently have 2 dataframes
df1=data.frame(q1 = c(1:3),
q2 = c("One" , "Two" , "Three") ,
q3 = c(100,231,523),
q4 = c("red", "green", "blue"),
q1.2 = c(20:22),
q2.2 = c("Six" , "Ten" , "Twenty") ,
q3.2 = c(5,900,121),
q4.2 = c("purple", "yellow", "white"))
df2=data.frame(x1 = c("q1" , "q2.1" , "q3.2" , "q4.2") ,
x2 = c("q2" , "q3" , "q3.3" , "q4.4") ,
x3 = c("q3" , "q2.4" , "q3.3" , "q4.6"),
x4 = c("q4" , "q3.6" , "q3.3" , "q4.2"))
I need to create 4 different tables. The headers of these tables are each of the rows included in df2 while the observations have to be obtained from df1. As you noticed, some of the headers included in df2 do not exist in df1. I want my 4 tables to include all 4 headers (whether they exist or not) and if it doesnt, the its data should be blank.
I am currently using this code
for (i in 1:nrow(df2)) {
colnames(df2)<- df2[i,]
tabla_temp = df1[intersect(names(df1), names(df2))]
tname <- paste0("tabla_", i)
assign(tname, tabla_temp)
rm(tabla_temp)
}
I get my loop working but i get tables with different amounts of columns (only those which exist in df1).
Any idea how i can get my loop to create same size tables with non existing headers to have blank obs instead?