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?
I have a data frame called School_Behavior.
I have a column "sex" with responses as
How do I only change the 1's and 2's in this column?
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)