0

I have a data frame called School_Behavior.
I have a column "sex" with responses as

  • 1 for male,
  • 2 for female.

How do I only change the 1's and 2's in this column?

zx485
  • 26,827
  • 28
  • 51
  • 55
Andrea H
  • 9
  • 1
  • 2

1 Answers1

0

We can use do that with indexing. Assuming that the 'sex' is character column, convert it to numeric, use that as index to pass the string vector for replacement

df1$Sex <- c("Male", "Female")[as.numeric(df1$Sex)]

df1 <- data.frame(Sex = c("1", "2", "1", "2", "2"), stringsAsFactors = FALSE)
akrun
  • 789,025
  • 32
  • 460
  • 575