[R] Handling missing values

David Winsemius dwinsemius at comcast.net
Wed Sep 28 19:50:14 CEST 2011


On Sep 28, 2011, at 11:49 AM, Samir Benzerfa wrote:

> Hi everyone,
>
>
>
> I've got a question concerning missing values in R.
>
>
>
> I'm looking for an R code which removes all NA values in my  
> data.frame. I
> found many answers about how to remove rows or columns which contain  
> NA's.
> My problem however is a bit different. I have a list of vectors.  
> Each vector
> contains some NA Values that I want to remove. So, at the end all  
> vectors
> will have different dimensions.

Then it will no longer be a data.frame.

> My final goal is to apply a function(x) to
> all of the vectors.

If the function has an na.rm= argument tha tyou cna set to TRUE, it  
may not be necessary to do all of this.

> Any hints how to do this? Should I change the
> class=data.frame of my list of vectors? Below I made a simple  
> example how my
> table looks like and how my result should look like.
>

lis.tbl <- lapply(tbl, function(x) x[!is,na(x)] )

You can then use lapply to get your results from your function:

yourres <- lapply(lis.tbl, yourfunc)

>
>
>
> My table:
>
>                V1          V2
>
> 1             1.2          2.1
>
> 2             1.3          2.2
>
> 3             NA         4.5
>
> 4             NA         1.5
>
> 5             1.9          NA
>
>
>
> My intended result:
>
>                V1          V2
>
>                1.2          2.1
>
>                1.3          2.2
>
>                1.9          4.5
>
>                                1.5
>
>
>
>
>
> Many thanks in advance & best regards
>
> S.B.
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list