r - Change levels from a variable -


i doing little function in order change levels of variables(factors) data set other levels come data set (both same name of variables).

the main code line solve problem be:

data1$variable <- factor(data1$variable, levels= levels(data2$variable)) a<- factor(c(1,2,3,4)) b<- factor(c(1,2,3)) a<- factor(a, levels=levels(b)) [1] 1    2    3    <na> 

and adding loop going through columns done with:

list_levels_d2 <- sapply(d2, levels) names_d1 <- colnames(d1) for(i in 1:length(names_d1)){      d1[, names_d1[i]] <- factor(d1[, names_d1[i]], levels= list_levels_d2[[i]])    } 

basically wanted ask if can figure out how make same avoiding loop "for" using sapply? apply?

i can´t use this:

function(x, y){     sapply(x, factor, levels= levels(y[, names(x)]))      } 

because in part levels(y[, names(x)]), x not taking 1 name takes whole array of names , value null

initial levels of first dataset ('d1')

 lapply(d1, levels)  #$v1  #[1] "a" "c" "e"   #$v2  #[1] "a" "c" "d" "e"   #$v3  #[1] "c" "d" "e" 

we can try map loop on corresponding columns of both datasets ('d1' , 'd2') , change levels of each column corresponding column level of second dataset.

d1[] <- map(function(x,y) {factor(x, levels=levels(y))}, d1, d2[names(d1)]) 

or option using lapply loop on column names

d1[] <- lapply(names(d1), function(x) {factor(x, levels=levels(d2[,x]))                                       }) 

after change

 lapply(d1, levels)  #$v1  #[1] "b" "c" "d" "e" "f" "g" "h"   #$v2  #[1] "a" "b" "c" "d" "f" "g"   #$v3  #[1] "b" "c" "d" "e" "f" "g" "h" 

which same levels of second dataset

 lapply(d2[names(d1)], levels)  #$v1  #[1] "b" "c" "d" "e" "f" "g" "h"   #$v2  #[1] "a" "b" "c" "d" "f" "g"   #$v3  #[1] "b" "c" "d" "e" "f" "g" "h" 

data

set.seed(28) d1 <- as.data.frame(matrix(sample(letters[1:5], 5*3,                  replace=true), ncol=3)) set.seed(34) d2 <- as.data.frame(matrix(sample(letters[1:8], 10*3,                      replace=true), ncol=3)) set.seed(429) d2 <- d2[sample(ncol(d2))]  

Comments