[R] Filling empty List in a FOR LOOP
ilai
keren at math.montana.edu
Sun Apr 1 23:08:23 CEST 2012
On Sun, Apr 1, 2012 at 9:59 AM, michaelyb <cel81009759 at gmail.com> wrote:
> Basically I need to read the data from an external source using many R
> commands.
> The problem is that sometimes the source is empty, and I get a list with
> empty values, and I need to substitute them by "NA".
> It is def. not hard, I just don't see how....
> In the prvious post, the first example had values on the source, but the
> second example didn't....
And neither was reproducible as is this post. There is more than one
way to get an "empty" list. I find R-list helpers are good but lacking
in extrasensory perception skills. Frustrating, but if you insist on
not following the posting guide, then you'll just have to figure it
out for yourself.
lapply(vector('list',2),function(ll) ifelse(is.null(ll),NA,ll)) # works
lapply(list("","foo"),function(ll) ifelse(is.null(ll),NA,ll)) # fail
lapply(list("","foo"),function(ll) ifelse(nchar(ll)<1,NA,ll)) # works
# ...
# from your example 1 seems like you're expecting a matrix, so maybe
you need nrow(ll)
# etc.
HTH
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Filling-empty-List-in-a-FOR-LOOP-tp4522694p4523731.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