[R] Deleting NA in list()

jim holtman jholtman at gmail.com
Mon Aug 25 13:56:51 CEST 2008


The only problem with this solution is what happens if the element has
more than one NA:

> lt <- list(a = 1:3, b = NA, c = letters[1:3], d = NA)
> is.na(lt)
    a     b     c     d
FALSE  TRUE FALSE  TRUE
> lt <- list(a = 1:3, b = c(NA,NA,NA), c = letters[1:3], d = NA)
> is.na(lt)
    a     b     c     d
FALSE FALSE FALSE  TRUE
>

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



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?



More information about the R-help mailing list