[R] Deleting NA in list()

jim holtman jholtman at gmail.com
Sat Aug 23 03:13:28 CEST 2008


Here is one way of doing it:

> lt
$a
[1] 1 2 3

$b
[1] NA

$c
[1] "a" "b" "c"

$d
[1] NA

> lt[!unlist(sapply(lt, function(x) any(is.na(x[1]))))]
$a
[1] 1 2 3

$c
[1] "a" "b" "c"

>


On Fri, Aug 22, 2008 at 2: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.
>



-- 
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