Replace values in column by factor level

谁说我不能喝 提交于 2019-12-02 09:01:11

We can loop over the columns and replace the levels using %in%

df1[] <- lapply(df1, function(x) {
            levels(x)[levels(x) %in% c("Yes!", "Yay")] <- "Yes"
            levels(x)[levels(x) %in% c("Nope", "Nah")] <- "No"
          x
        })

To drop the unused levels we can use droplevels

df2 <- droplevels(df1)

But, based on the assignment we did earlier, it would be taken care off.

df1
#   Col1 Col2 Col3
#1   Yes   No   No
#2   Yes  Yes   No
#3    No   No   No
#4    No   No   No
#5    No  Yes   No
#6    No   No   No
#7   Yes  Yes   No
#8    No  Yes   No
#9    No   No   No
#10  Yes  Yes   No


str(df1)
#'data.frame':   10 obs. of  3 variables:
#$ Col1: Factor w/ 2 levels "No","Yes": 2 2 1 1 1 1 2 1 1 2
#$ Col2: Factor w/ 2 levels "No","Yes": 1 2 1 1 2 1 2 2 1 2
#$ Col3: Factor w/ 1 level "No": 1 1 1 1 1 1 1 1 1 1

data

set.seed(24)
df1 <- data.frame(Col1 = sample(c("Yes", "Yes!", "Yay", "Nope", "Nah", "No"),
         10, replace=TRUE),

               Col2 = sample(c("Yes", "Yes!", "Yay", "Nope", "Nah", "No"), 10, replace=TRUE),
               Col3 = sample(c("Nope", "Nah", "No"), 10, replace=TRUE)
             )
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!