简体   繁体   中英

Make new Levels of categorical variable in new Variable Column in R

I am a newbee to R so got stuck here.. I have a categorical data

levels(df$SO)
"SO1", "SO2","SO3","SO4","SO5","SO6",SO7",SO8"

I want to re-categorize these levels as follows BUT SAVE THEM AS NEW COLUMN (df$newSO) IN SAME DATAFRAME.

levels(df$newSO)
"Unknown", "Known","Disease","Control"

Here Unknown is made of SO1 and SO2 levels, Known consists of SO3 and SO4. Disease contains SO5,SO6 and SO7. Control contains S8. I am using following

levels(df$SC)[levels(df$SC)%in%c("SOC1","SOC2")] <- "Unknown"

But it is renaming the levels in same column (df$SO). I want previous column intact while creating new column of new levels. How to do this in R?

您可以尝试以下操作

df$newSO <- ifelse(df$SO %in% c("SOC1", "SOC2"), "Unknown", "Known")

dplyr and forcats solution:

library(dplyr)
library(forcats)

example <- data.frame(SO = factor(c("SO1", "SO2", "SO3", "SO4",
                                    "SO5", "SO6", "SO7", "SO8")))

result <- example %>%
  mutate(newSO = fct_collapse(SO,
                              Unknown = c("SO1", "SO2"),
                              Known = c("SO3", "SO4"),
                              Disease = c("SO5", "SO6", "SO7"),
                              Control = "SO8"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM