I have a data frame in which missing values exist, and I need to
recode the string "missing" to a missing value. For the example, let's
assume I cannot do this while reading it in. Even though this has been
discussed extensively, I'm still a little confused about when to index
with "which" and when to use logical indexing.

Below is what I have done. Is there a more R-appropriate way to do this? Thanks.


# create data with missing values

myData <- head(mtcars)
myData[c(1,2),'cyl'] <- "missing"
is.na(myData[3,'cyl']) <- TRUE
myData[c(1,2,5),'disp'] <- "missing"
myData


# loop through columns to replace "missing"

myColNames <- colnames(myData)

for (myCol in myColNames) {
   NA_index <- which(myData[,myCol] =="missing")

    # which creates problems when no columns have "missing"

   if (length(NA_index) > 0) {
      is.na(myData[NA_index,myCol]) <- TRUE
   }
}

myData

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to