[R] problem in R

R. Michael Weylandt michael.weylandt at gmail.com
Wed Jan 4 17:21:09 CET 2012


I'm not a huge fan of this sort of solution because it doesn't make
sense for non-vector-shaped (i.e., matrix or data.frame) data. It only
works here because the matrix produced is a special 1xN case.

E.g.,

# Set up some data (and yes, I realize I'm using the trick I'm
speaking out against, but c'est la vie)
testData <- matrix(rnorm(20), 5)
testData[c(3,6,7)] <- NA

testData[!is.na(testData)] # No longer a matrix so any information
stored in the shape is lost

# I'd prefer something like
testData[complete.cases(testData), ]

or perhaps these sorts of helper functions:

completeRows <- function(x){
    x[rowSums(is.na(x)) == 0L, ]
}

completeCols <- function(x){
    x[, colSums(is.na(x)) == 0L]
}

both of which I believe will work for matrices & data.frames. With a
little work, one could make them work for regular vectors as well to
have a general solution.

In general, however, you'll need to figure out what the correct
treatment of NA's for your problem is: some functions like lm(), sum()
etc. can deal with NAs so you don't need to pre-change your input.
(Protip: with lm(), I find people almost always prefer na.exclude to
the default na.omit)

Michael

On Wed, Jan 4, 2012 at 6:01 AM, iliketurtles <isaacm200 at gmail.com> wrote:
> data<-matrix(rnorm(10))
> data[c(1,4,6)]<-NA
> print(data)
> data<-matrix(data[!is.na(data)])
> print(data)
>
> -----
> ----
>
> Isaac
> Research Assistant
> Quantitative Finance Faculty, UTS
> --
> View this message in context: http://r.789695.n4.nabble.com/problem-in-R-tp4260254p4260976.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list