Double for loop with ifelse() is not working properly in R

250 views Asked by At

I am trying to run through each columns of my dataframe and convert "unknown" values to NAs. I tried the following code:

for (i in seq(length(df))) {
 for (j in seq(nrow(df))) {
      ifelse(df[,i][j] == "unknown", NA, df[,i][j])
 }
}

It, however, has not changed any values. The columns I am trying to alter are factors so I also tried:

for (i in seq(length(df))) {
 x <- class(df[,i])
 as.character(df[,i])
 for (j in seq(nrow(df))) {
      ifelse(df[,i][j] == "unknown", NA, df[,i][j])
 }
 class(df[,i]) <- x
}

to no avail. There is no error obtained and the code appears to run without problem; only the values remain as "unknown.

2

There are 2 answers

0
Tim Biegeleisen On BEST ANSWER

We can try:

df[df == "unknown"] = NA

This assumes that all your columns are character and not factor.

0
IRTFM On

Running ifelse does not change the values of any of its arguments. You would have needed to assign the result back to the column of df. And doing it by row doesn't make any sense because ifelse is designed to be used on a vector, so instead it might be:

for (i in seq(length(df))) {
       df[,i] <- ifelse(df[,i] == "unknown", NA, df[,i] )
 }
}

Which still isn't optimal in light of the much more efficient strategy illustrated by @TimBiegeleisen, but at least you can study this to see how to improve your understand of using for-loops and ifelse when using R.