I have two data sets (csv files i.e. 1000 columns and 200 columns) and I want to dynamically add the some of column names of the second data set to my first data set, based on the the exactness of two column elements in both data set. (Confused: Look at the expected and actual result expected as below)
I tried putting in two for loops to compare the element by element. As soon as column element are matched (matching criteria), pick and drop that element into a vector. Repeat the process for all the elements in the inner for loop --> once the inner for loop is completed, transpose the vector and convert it to column vector--> add the column to that particular row based on the outer for loop row value
df_1$Res_1 <- NA
df_1$Res_1 <- as.character(df_1$Res_1)
df_1_1 <- rep(NA,10)
df_1_1 <- as.character(df_1_1)
c = 1
for (p in 1:nrow(df_1)){
for(q in 1:nrow(df_2)){
if((df_1$Mat_1[p] == df_2$Mat_2[q]) {
df_1_1[c] <- df_2$Res[q]
c = c+1
}
df_1_1 <- t(df_1_1)
df_1$Res_1[p,] <- df_1_1[] #not sure how to add the remaining #columns like Res_2 and Res_3 etc
}
c = 1
}
Error is
df_1$Res_1[p,] <- df_1_1[]:
incorrect number of subscripts on matrix