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:
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
dput(d)
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[[1]]))
unlist(lapply(d, getDate))
[1] "20110405" "20110405" "20110405" "20110405" "20110405" "20110405"
Isn't there an easier way to do this?
Thanks,
Santosh
______________________________________________ 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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/