[R] List elements of NULL to value

Phil Spector spector at stat.berkeley.edu
Mon Nov 29 21:58:07 CET 2010


Does

     A = lapply(A,function(x)if is.null(x) 0 else x)
or
     for(i in 1:length(A))if(is.null(A[[i]]))A[i] = 0

do what you want?

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Mon, 29 Nov 2010, LCOG1 wrote:

>
> Hi everyone,
>    I am posting this because i know its easy and i cant for the life of me
> figure out how to do it though i have tried and through a ridiculously
> complex loop made it happen.  I need to convert some list elements of NULL
> value to 0s so they mesh with my data frame properly.  So for
>
> A<-list(1,NULL)
>
> returns
>
> [[1]]
> [1] 1
>
> [[2]]
> NULL
>
> Would instead return
>
> [[1]]
> [1] 1
>
> [[2]]
> [1] 0
>
> The nabble posts seem to indicate dealing with NULLs can be tricky so i am
> attributing my lack of success to that, mainly so i can live with myself and
> asking the list such a simple question.  Sorry again guys.
>
> JR
> -- 
> View this message in context: http://r.789695.n4.nabble.com/List-elements-of-NULL-to-value-tp3064384p3064384.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