[R] access an element of a list without looping

Greg Snow 538280 at gmail.com
Thu Jul 3 21:54:46 CEST 2014


You could use

which( sapply(l, length) == 2 )

but that still uses a loop internally.

On Thu, Jul 3, 2014 at 1:35 PM, carol white <wht_crl at yahoo.com> wrote:
> Hi,
> Is there any way to access an element of a list without looping over the list nor using unlist? Just to avoid parsing a very long list.
>
>
> For ex, how to find a vector of a length 2 in a list without using a loop?
>
> l = list (c(1), c(2,3), c(1,2,3))
> for (i in 1:length(l))
>     if(length(l[[i]]==2){
>         print (i)
>         break
>     }
>
> Thanks
>
> Carol
>
>         [[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.
>



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list