Reputation: 353
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? cheers
Upvotes: 0
Views: 57
Reputation: 887048
In tidyr
, there is unite
to concatenate columns
library(tidyr)
school_info %>%
unite(simple, gender, semester, remove=FALSE)
# simple semester gender score
#1 F_01 01 F 152
#2 M_02 02 M 190
Upvotes: 1
Reputation: 13274
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.
Upvotes: 2