r - Assigning value labels with factor() using mapply() -
i'm having trouble assigning value labels lists numeric variables. i've got dataset (in form of list()
) containing eleven variables. first 5 variables each have individual value levels, last 6 each use same 1-5 scale. created lists value labels each of first 5 variables , 1 scale. automatically assign labels lists variables.
i've put eleven variables in list able use mapply()
.
here's example of current state:
# example variables: <- c(1,2,3,4) # individual variable b <- c(1,2,2,1) # individual variable b c <- c(1,2,3,4,5) # variable c using scale d <- c(1,2,3,4,5) # variable d using scale mydata <- list(a,b,c,d) # example value labels: lab.a <- c("these", "are", "value", "labels") lab.b <- c("some", "more") lab.c <- c("and", "those", "for", "the", "scale") labels.abc <- list(lab.a, lab.b, lab.c) # assigning labels in 2 parts part.a <- mapply(function(x,y) factor(as.numeric(x), labels = y, exclude = na), mydata[1:2], labels.abc[1:2]) part.b <- mapply(function(x,y) factor(as.numeric(x), labels = y, exclude = na), mydata[3:4], labels.abc[3])
apart not being able combine 2 parts, major problem output format. mapply()
gives result in form of matrix, need again list containing specific variables.
so, question is: how can assign value labels in automated procedure , result again list of variables, contain labeled information instead of numerics?
i'm quite lost here. approach mapply()
doable, or on wrong track?
thanks in advance! please comment if need further information.
problem solved!
thanks @agstudy pointing out simplify = false argument, prevents mapply() reducing result matrix.
the correct code is
part.a <- mapply(function(x,y) factor(as.numeric(x), labels = y, exclude = na), mydata[1:2], labels.abc[1:2], simplify = false)
this provides same format of output put in.
Comments
Post a Comment