我有一个名字基于Salutation的列我需要分类并分配一个新值,无论这个人是男性还是女性?我该怎么做?
答案 0 :(得分:1)
dat <- "sal,name
Dear Mrs.,Jones
Dear Mr.,Smith
Dear Mr.,Black"
dat <- read.table(text=dat, header=TRUE, sep=",")
dat$gender <- ifelse(grepl(pattern = "Mr\\.",dat$sal), "Male", "Female")
dat
# sal name gender
# 1 Dear Mrs. Jones Female
# 2 Dear Mr. Smith Male
# 3 Dear Mr. Black Male