Unexpected results from ifelse

后端 未结 1 514
清酒与你
清酒与你 2021-01-21 03:59

I am getting unexpected results from the ifelse function :

vector <- factor(c(\'x\', \'x\', \'y\', \'z\'), levels = c(\'x\', \'y\', \'z\'))
ifel         


        
相关标签:
1条回答
  • 2021-01-21 04:45

    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
    
    0 讨论(0)
提交回复
热议问题