Hi, This is probably a small query but one I'm struggling with: I have a list in which I had elements which were NA, I removed them, by doing: list2 <- lapply(list, na.omit), However this leaves the element there with 'character(0)' in place as well as attributes: e.g. [[978]] character(0) attr(,"na.action") [1] 1 attr(,"class") [1] "omit" I want to get rid of these elements/positions in the list, since a function is supposed to sample the list for elements (each element is a collection of about 20 numbers each). Thanks, Ben W. UEA (ENV) - b.ward@uea.ac.uk [[alternative HTML version deleted]]
lapply always yields one output for every input. Try using a for loop and only
copying the element once you know you want it. You will need an output index
counter that is separate from the for loop index, incremented only when you copy
an element.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
"Benjamin Ward (ENV)" <B.Ward at uea.ac.uk> wrote:
>Hi, This is probably a small query but one I'm struggling with: I have
>a list in which I had elements which were NA, I removed them, by doing:
>list2 <- lapply(list, na.omit),
>
>However this leaves the element there with 'character(0)' in place
as
>well as attributes:
>
>e.g.
>[[978]]
>character(0)
>attr(,"na.action")
>[1] 1
>attr(,"class")
>[1] "omit"
>
>
>I want to get rid of these elements/positions in the list, since a
>function is supposed to sample the list for elements (each element is a
>collection of about 20 numbers each).
>
>Thanks,
>
>Ben W.
>
>UEA (ENV) - b.ward at uea.ac.uk
>
>
>
> [[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.
Hi, Benjamin, have you tried for your list with NA-components to use is.na() as follows (where x is assumed to be your list)? x[ !is.na(x)] Hth -- Gerrit On Tue, 29 Jan 2013, Benjamin Ward (ENV) wrote:> Hi, This is probably a small query but one I'm struggling with: I have a > list in which I had elements which were NA, I removed them, by doing: > list2 <- lapply(list, na.omit), > > However this leaves the element there with 'character(0)' in place as > well as attributes: > > e.g. > [[978]] > character(0) > attr(,"na.action") > [1] 1 > attr(,"class") > [1] "omit" > > > I want to get rid of these elements/positions in the list, since a > function is supposed to sample the list for elements (each element is a > collection of about 20 numbers each). > > Thanks, > > Ben W. > > UEA (ENV) - b.ward at uea.ac.uk > > > > [[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.
Hi,
May be this helps:
x<- list(1:5,NA,20:25,5)
names(x)<-1:4
?fun1<-function(lst){
?lst[lapply(lapply(lst,Filter,f=Negate(is.na)),length)!=0]}
fun1(x)
#$`1`
#[1] 1 2 3 4 5
#
#$`3`
#[1] 20 21 22 23 24 25
#
#$`4`
#[1] 5
#or
x[lapply(lapply(x,na.omit),length)!=0]
A.K.
----- Original Message -----
From: Benjamin Ward (ENV) <B.Ward at uea.ac.uk>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Monday, January 28, 2013 9:21 PM
Subject: [R] NA and Character(0) in List Element
Hi, This is probably a small query but one I'm struggling with: I have a
list in which I had elements which were NA, I removed them, by doing: list2
<- lapply(list, na.omit),
However this leaves the element there with? 'character(0)' in place as
well as attributes:
e.g.
[[978]]
character(0)
attr(,"na.action")
[1] 1
attr(,"class")
[1] "omit"
I want to get rid of these elements/positions in the list, since a function is
supposed to sample the list for elements (each element is a collection of about
20 numbers each).
Thanks,
Ben W.
UEA (ENV) - b.ward at uea.ac.uk
??? [[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.