[R] Selecting data from list object
jwiley.psych at gmail.com
Thu Apr 7 08:37:19 CEST 2011
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
which is equivalent to j = 1, i = 1. There is also list method for
as.data.frame so in your example, you could do:
so long as your data was conformable.
On Wed, Apr 6, 2011 at 11:14 PM, santosh <santosh.srinivas at gmail.com> wrote:
> Hello Group,
> Is there a simpler way to get data out of a list object? (like in data
> frame without using the apply functions)
> I have the following dataset
> list(c("20110405", "092102"), c("20110405", "092538"), c("20110405",
> "093458"), c("20110405", "101124"), c("20110405", "102041"),
> c("20110405", "103659"))
> I extracted my data like this:
> getDate <- function(x)(unlist(x[]))
> unlist(lapply(d, getDate))
>  "20110405" "20110405" "20110405" "20110405" "20110405" "20110405"
> Isn't there an easier way to do this?
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Ph.D. Student, Health Psychology
University of California, Los Angeles
More information about the R-help