Juliet Hannah
2011-Jun-28 19:39 UTC
[R] indexing with which, logical indexing, and missing values
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