Chris Chris - 1 year ago 70
R Question

How to create a factor variable from values from two other columns

I want to add a new categorical variable based on values from two other columns.

In the example below, I want to create the new variable "simple" using the semester and gender info.

semester gender score simple
01 F 152 F_01
02 M 190 M_02

Can I do this with dplyr?

Answer Source

Try the following:

school_info <- data.frame(semester=c("01","02"), gender = c("F","M"), score = c(152, 190))

school_info <- school_info %>% 
    mutate(simple = paste(gender,semester,sep = "_"))

You could have used base R without the need for external libraries:

school_info$simple <- paste(school_info$gender,school_info$semester,sep = "_")

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download