Reputation: 107
as a part of a project, I was given a medical dataframe for analasys. As part of the analasys, I had to fix the variable called "Gender", due to an error created by reading the data:
datS$Gender <- ifelse(datS$Gender == "¦ֳ«ֳ","Male", ifelse(datS$Gender == "°ֳ·ֳ¡ֳ₪","Female", datS$Gender))
However, doing so, changed the values of the variable to:
1 2 3
4461 3373 479
While those numbers are correct, their lable isn't. I attempted to fix that using the replace function:
datS$Gender <- replace(datS$Gender, c("1","2", "3"), c("Male", "Female", "censored"))
However, doing so reasulted in thje following error message:
Error in `$<-.data.frame`(`*tmp*`, Gender2, value = c("3", "3", "3", "3", :
replacement has 8316 rows, data has 8313
The correct number of observasions is 8313, and I don't know what had caused the error. Can you please shed some light over why does this error occure? Many thanks in advance!
Upvotes: 0
Views: 4272
Reputation: 886938
We can convert the factor
columns to character
with type.convert
dat <- type.convert(dat, as.is = TRUE)
Upvotes: 0
Reputation: 915
Try a dplyr
approach to replacing the strange values:
library(dplyr)
dat %>%
dplyr::mutate(
FixedGender = dplyr::case_when(Gender == "¦ֳ«ֳ" ~ "Male",
Gender == "°ֳ·ֳ¡ֳ₪" ~ "Female",
TRUE ~ as.character(Gender))) %>%
select(Gender, FixedGender) # This line is just to compare the two side-by-side
Upvotes: 1
Reputation: 388797
Maybe you have columns of factor type. Try turning it to character and then try your ifelse
code.
datS$Gender <- as.character(datS$Gender)
You can also have a look at dplyr::recode
to recode the values since replace
cannot change multiple values.
Upvotes: 1