[R] Accessing the elements of a list

Bill.Venables at csiro.au Bill.Venables at csiro.au
Fri Feb 1 10:49:59 CET 2008


 
I find it hard to imagine you need to do it without using sapply.  If
you get over your phobia, you might like to try

sapply(v, "[", 3)

If the elements of the list are all the same length, as in your example,
you could do

data.frame(v)[3, ]

I think we need to know more about why you think you can't use sapply
before we can really help you, though.

Bill Venables.


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Shubha Vishwanath Karanth
Sent: Friday, 1 February 2008 7:05 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Accessing the elements of a list

Hi R,

 

I wanted to know how do we access the elements of a list. In particular,

 

v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,3333,4,5))

 

I want to access all the thirds items of the elements of the list. i.e.,
I want to access the elements, 3,33,333,3333. This can be done through
sapply as:

 

sapply(v,function(x) x[3])

 

But I need to access this without using 'sapply'. Like in a data frame,
we do df[2,3] etc.... Any ideas?

 

 

BR, Shubha

This e-mail may contain confidential and/or privileged
i...{{dropped:13}}

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



More information about the R-help mailing list