Recode/relevel data.frame factors with different levels

后端 未结 4 1154
予麋鹿
予麋鹿 2020-12-25 08:44

Each time when I have to recode some set of variables, I have SPSS recode function in mind. I must admit that it\'s quite straightforward. There\'s a similar recode

4条回答
  •  长发绾君心
    2020-12-25 09:05

    If you complete the factor levels you're good to go:

    df <- data.frame(x=factor(c(2,4,5,6)))
    df$x <- factor(df$x, levels = 7:1)
    table(df$x)
    
    7 6 5 4 3 2 1 
    0 1 1 1 0 1 0 
    

提交回复
热议问题