0

My data set has a variable "Gender" with levels "Man" and "Woman". I want to change "Man" and "Woman" into "M" and "W" respectively. How can I do it in R?

sara
  • 21
  • 2

2 Answers2

1

One solution is to use a factor and change the labels of the levels:

my_data <- data.frame(gender = c("Man", "Woman"))
my_data$gender = factor(my_data$gender, levels = c("Man", "Woman"), labels = c("M", "W"))
nrennie
  • 1,877
  • 1
  • 4
  • 14
  • Thanks, I did it in my data set but it vary all column's gender into NA. I don't know what the problem is – sara Aug 16 '23 at 13:21
  • Is your column already a factor? Are the values definitely capitalised? If you share a sample of your data using `dput()` we're more likely to be able to help. – nrennie Aug 16 '23 at 14:12
0

a solution using ifelse:

factor(ifelse(YOUR_VECTOR=="Man","M","W"))
badgorilla
  • 96
  • 3