I have two dataframes: dataframe_x
and dataframe_y
now i want to loop through both of them at the same time to check if dataframe_x
vector_x_2
matches vector_y_1
from dataframe_y
if so i want the matched row from dataframe_y
in my dataframe_x
. Right now my loop only tries to fills the 3rd column which is vector_y_1
but ideally it would copy the whole row into dataframe_x
.
This is the code I came up with Code:
vector_x_1 <- c("Max.1","Max.1","Max.2")
vector_x_2 <- c("BX.1","BX.6","BX.5")
dataframe_x <- data.frame(cbind(vector_x_1,vector_x_2))
vector_y_1 <- c("BX.1","BX.3","BX.3","BX.10","BX.15")
vector_y_2 <- c("9,90","9,80","9,80", "7,90", "4,70")
vector_y_3 <- c("A","B","C", "D", "E")
vector_y_4 <- 1:5
dataframe_y <- data.frame(cbind(vector_y_1,vector_y_2, vector_y_3,vector_y_4))
dataframe_x <- dataframe_x %>% mutate("vector_y_1"="","vector_y_2"="","vector_y_3"="","vector_y_4"="")
for (i in nrow(dataframe_x)) {
for (k in nrow(dataframe_y)) {
if(dataframe_x[i,2] == dataframe_y[k,1]) {
dataframe_x[i,3] <- dataframe_y[k,1]
}
else {
dataframe_x[i,3] <- "no match"
}
}
}
This is probably a really basic question but i do not understand why my loops are not working.
Thanks for help!