Kumarjit Pathak
Kumarjit Pathak

Reputation: 1

How to classify gender based on salutation

I have a column with names based on Salutation I need to classify and assign a new value whether the person is male or female? how do I do it?

Upvotes: 0

Views: 154

Answers (1)

Dominic Comtois
Dominic Comtois

Reputation: 10411

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

Upvotes: 1

Related Questions