Hi,
You can also use:
jlist[unlist(lapply(jlist,length))>0]
#[[1]]
#[1] 1 0
#[[2]]
#[1] 1 2 3
A.K.
From: Gonçalo Ferraz
To: arun
Sent: Thursday, November 8, 2012 9:59 AM
Subject: Re: [R] Accessing selected elements of a list
Thanks! I think one qu
ing selected elements of a list
Hi,
If I have a vector:
junk <- c(2,0,0,3,0)
and want to access, say, all the elements that are greater than zero. I just do:
junk[which(junk>0)]
Now, If I have a list:
jlist <- list(NULL,c(1,0),NULL,c(1,2,3), NULL)
and want to access all the elemen
try this (use '[' for indexing)
> jlist <- list(NULL,c(1,0),NULL,c(1,2,3), NULL)
> jlist
[[1]]
NULL
[[2]]
[1] 1 0
[[3]]
NULL
[[4]]
[1] 1 2 3
[[5]]
NULL
> which(sapply(jlist, length) > 0)
[1] 2 4
> jlist[sapply(jlist, length) > 0]
[[1]]
[1] 1 0
[[2]]
[1] 1 2 3
>
On Thu, Nov 8, 2012 at 9:42
Hello,
Just try
jlist[ sapply(jlist,length) > 0 ]
Hope this helps,
Rui Barradas
Em 08-11-2012 14:42, Gonçalo Ferraz escreveu:
Hi,
If I have a vector:
junk <- c(2,0,0,3,0)
and want to access, say, all the elements that are greater than zero. I just do:
junk[which(junk>0)]
Now, If I hav
Hi,
If I have a vector:
junk <- c(2,0,0,3,0)
and want to access, say, all the elements that are greater than zero. I just do:
junk[which(junk>0)]
Now, If I have a list:
jlist <- list(NULL,c(1,0),NULL,c(1,2,3), NULL)
and want to access all the elements that have length greater than zero, I
5 matches
Mail list logo