Reputation: 1
I have several factors with levels "No answer" and "don't know" which I want to define as missing. However, the function I wrote to achieve this is not working and I cannot figure out why. Example:
y <- factor(c("a", "b", "c", "Don´t know", "No answer"))
z <- factor(c("a", "b", "c", "Don´t know", "No answer"))
y
[1] a b c Don´t know No answer
Levels: a b c Don´t know No answer
Defining levels as missing for a single variable does work
levels(y)[levels(y)=="Don´t know"|levels(y)=="No answer"]<- NA
y
[1] a b c <NA> <NA>
Levels: a b c
However, applying a function does not.
nafac <- function(x)
{
levels(x)[levels(x)=="Don´t know"|levels(x)=="No answer"]<- NA
}
nafac(z)
z
[1] a b c Don´t know No answer
Levels: a b c Don´t know No answer
What is the problem with the function? Thank you!
Upvotes: 0
Views: 234
Reputation: 2670
we can simply add return
to return updated vector;
nafac <- function(x){
levels(x)[levels(x)=="Don´t know"|levels(x)=="No answer"]<- NA
return(x)
}
nafac(z)
output;
a b c <NA> <NA>
Upvotes: 1