I have a dataset where I need to replace certain values in different columns with NA. The values that should be NA change in some columns due to the data being from a survey with different amount of possible answers. I know how to change the values like this:
ayw <- ayw %>%
mutate(ExperienceWMentorSRP_01 = ifelse(ExperienceWMentorSRP_01 == 5, NA, ExperienceWMentorSRP_01))
or like this:
ayw$ExperienceWMentorSRP_01 <- replace(ayw$ExperienceWMentorSRP_01, ayw$ExperienceWMentorSRP_01 ==5, NA)
So, there are some sections where I repeat this line of code (just different number after the underscore) for more than a dozen times. I feel like there is a more efficient way of doing this without having to change the column names manually everytime.
I tried making a function:
na.fun <- function(dataset, column, nanumber){
dataset$column[dataset$column == nanumber] = NA
}
na.fun(ayw, ExperienceWMentorSRP_01, 5)
but I get the following error:
! Assigned data <lgl>
must be compatible with existing data.
✖ Existing data has 155 rows.
✖ Assigned data has 0 rows.
I think I might be going in the wrong direction. I would still need to write it n times anyway unless I make a loop work. And I've tried doing it, as well. Like this:
for (row in mentorset){ #used `select() %>%` to make this subset(mentorset)so I didn't mess anything else
for (col in row){
ifelse(col == 5, NA, col)
}
}
But when I try to save the result each time using mentorset <-
and printing the result, it ends up with a data frame with only one value, e. g. '4'. I assume it saves the last iteration of the loop, and that's the reason.
How could I solve this problem? Am I better off just writing it manually?