Skip to content
Prev 256047 / 398506 Next

Selecting data from list object

Hi Santosh,

Try this:

sapply(d, `[[`, i = 1)

To answer your question about "without using the apply functions", I
think the answer is "not really".  Data frames are a type of list, so
if you can assume that it is reasonable to extract the same element
from every element of your list, that is for j = 1, ... n list
elements, and i = 1, ... , k elements in each list element j, if k is
identical across all n list elements, then you can simply reshape the
list into a (i, j) data frame and extract based on rows or columns.
To the extent that all i may not exist in all j, attempting to extract
the ith element from every j list element becomes questionable.  You
may know certain properties of your list (e.g., k varies across j, but
k >= 4, so it would always be defined for 1 <= i <= 4), that make what
you want to do logical and reliable, but there are not the general
methods as in data frames, matrices, or arrays for extracting based on
a particular dimension (all of row one, etc.).  For some things you
can use:

d[[c(1, 1)]]

which is equivalent to j = 1, i = 1.  There is also list method for
as.data.frame so in your example, you could do:

as.data.frame(d)

or

as.data.frame(d)[1, ]

so long as your data was conformable.

Cheers,

Josh
On Wed, Apr 6, 2011 at 11:14 PM, santosh <santosh.srinivas at gmail.com> wrote: