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.
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. >
Try this: replace(A, sapply(A, is.null), 0) On Mon, Nov 29, 2010 at 6:50 PM, LCOG1 <jroll@lcog.org> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Yes that works thank you, and as always I feel foolish for asking. You guys are great. Cheers, Josh Roll Lane Council of Governments Transportation Planner 541-682-2454 jroll at lcog.org www.lcog.org -----Original Message----- From: Phil Spector [mailto:spector at stat.berkeley.edu] Sent: Monday, November 29, 2010 12:58 PM To: ROLL Josh F Cc: r-help at r-project.org Subject: Re: [R] List elements of NULL to value 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-tp3064384 > p3064384.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. >