The levels in df1 that matches the lab_pt in the dataframe lookup_df I would like to replace with the corresponding leves in the second column of the lookup_df (this here: lab_en). But I want to keep the rest as it is. Thanks a lot!
--
Main dataframe
df1 <- data.frame(
num_var = sample(200, 15),
col1 = rep(c("onda","estrela","rato","caneta","ceu"), 3),
col2 = rep(c("muro","gato","pa","rato","ceu"), 3),
col3 = rep(c("surf","onda","dente","onda","sei"), 3),
col3 = rep(c("onda","casa",NA,"nao","net"), 3))
Lookeup data frame
lookup_df <- data.frame(
lab_pt = c("onda","estrela","rato","caneta","ceu"),
lab_en = c("wave","star","rat","pen","sky"))
I have tried this here below . It does the job, but the non matching information is transformed to NAs and this I don't want.
rownames(lookup_df) <- lookup_df$lab_pt
apply(df1[,2:ncol(df1)], 2, function(x) lookup_df[as.character(x),]$lab_en)
This post here is quite similar, but in that case all the levels are matchable, different from this here. Thanks a lot! Replace values in a dataframe based on lookup table