[R] Deleting NA in list()

jim holtman jholtman at gmail.com
Mon Aug 25 18:23:35 CEST 2008


That would work too.  Probably had the unlist in there from when I was
using 'lapply' at the time.

2. You can not be sure that it is a one element vector.  Also it was
unclear what should happen if only 'some' of the values of the vector
were NA.  If it was guarenteed that an element of the list was a
vector with only a single NA, then the original solution will work.
Just need some clarification as to the conditions that have to be
checked for.

On Mon, Aug 25, 2008 at 10:03 AM, Kenn Konstabel <lebatsnok at gmail.com> wrote:
> Umm ...
>
> On Mon, Aug 25, 2008 at 2:56 PM, jim holtman <jholtman at gmail.com> wrote:
>>
>> The only problem with this solution is what happens if the element has
>> more than one NA:
>>
>> [...]
>>
>> In that case, 'b' does not have the right answer.  That was the reason
>> for using 'all' to check for the condition:
>>
>> > unlist(sapply(lt, function(x) all(is.na(x[1]))))
>>    a     b     c     d
>> FALSE  TRUE FALSE  TRUE
>
>
> 1. Unlist is unnecessary here (sapply automatically simplifies its result).
> 2. Why use all() with one-element vector (x[1])?
>
> So I assume that what you meant is actually something like this:
>
> sapply(lt, function(x) all(is.na(x)))
>
>
> Kenn
>>
>>
>>
>> On Mon, Aug 25, 2008 at 3:33 AM, Kenn Konstabel <lebatsnok at gmail.com>
>> wrote:
>> > lt[!is.na(lt)] is a rather obvious way...
>> >
>> >
>> > On Fri, Aug 22, 2008 at 9:41 PM, Dong-hyun Oh <oh.dongh at gmail.com>
>> > wrote:
>> >
>> >> Dear useRs,
>> >>
>> >> I would like to know the way of deleting NA in list().
>> >>
>> >> Following is a example.
>> >>
>> >> lt <- list(a = 1:3, b = NA, c = letters[1:3], d = NA)
>> >>
>> >> for(i in length(lt):1) {
>> >>        if(is.na(lt[[i]])) lt[[i]] <- NULL
>> >> }
>> >>
>> >> How to simplify for() loop by using (l)apply family?
>> >>
>> >> Thank you in advance.
>> >>
>> >>
>> >>
>> >>
>> >> =========================================================
>> >> Dong-hyun Oh
>> >> Center of Excellence for Science and Innovation Studies
>> >> Royal Institute or Technology, Sweden
>> >> e-mail: oh.dongh at gmail.com
>> >> cel: +46 73 563 45 22
>> >>
>> >> ______________________________________________
>> >> 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.
>> >>
>> >
>> >        [[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.
>> >
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list