I am getting unexpected results from the ifelse
function :
vector <- factor(c(\'x\', \'x\', \'y\', \'z\'), levels = c(\'x\', \'y\', \'z\'))
ifel
I think you just want to use an if
statement. Not ifelse()
. The latter is meant to work with vectors and return a vector with length equal to that of the input. if you want to return a different number of elements for different conditions, just use if
.
vector <- factor(c('x', 'x', 'y', 'z'), levels = c('x', 'y', 'z'))
xx <- if(class(vector) == "factor") levels(vector) else unique(vector)
xx