I have a list that is made of lists of varying length. I wish to create a new vector that contains the last element of each list. So far I have used sapply to determine the length of each list, but I'm stymied at the part where I index the list to make a new vector containing only the last item of each list mylist = list(c(1,2,3),c("cat","dog"),c("x","y","z","zz")) # Create list last <- sapply(mylist,length) # Make vector with list lengths last_only <- mylist[[1:length(mylist)]][last] # Crash and burn trying to make new vector with last items! How do I do this last step? Dr. Seth W. Bigelow Biologist, USDA-FS Pacific Southwest Research Station 1731 Research Park Drive, Davis California sbigelow@fs.fed.us / ph. 530 759 1718 [[alternative HTML version deleted]]
On May 25, 2011, at 3:25 PM, Seth W Bigelow wrote:> I have a list that is made of lists of varying length. I wish to > create a > new vector that contains the last element of each list. So far I > have used > sapply to determine the length of each list, but I'm stymied at the > part > where I index the list to make a new vector containing only the last > item > of each list > > mylist = list(c(1,2,3),c("cat","dog"),c("x","y","z","zz")) # > Create > list > > last <- sapply(mylist,length) # Make vector with list lengths > > last_only <- mylist[[1:length(mylist)]][last] # Crash and burn > trying to > make new vector with last items! > > How do I do this last step?> lapply(mylist, tail, 1) [[1]] [1] 3 [[2]] [1] "dog" [[3]] [1] "zz" > unlist(lapply(mylist, tail, 1)) [1] "3" "dog" "zz"> > > Dr. Seth W. Bigelow > Biologist, USDA-FS Pacific Southwest Research Station > 1731 Research Park Drive, Davis California > sbigelow at fs.fed.us / ph. 530 759 1718 > [[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.David Winsemius, MD West Hartford, CT
On May 25, 2011, at 2:25 PM, Seth W Bigelow wrote:> I have a list that is made of lists of varying length. I wish to create a > new vector that contains the last element of each list. So far I have used > sapply to determine the length of each list, but I'm stymied at the part > where I index the list to make a new vector containing only the last item > of each list > > mylist = list(c(1,2,3),c("cat","dog"),c("x","y","z","zz")) # Create > list > > last <- sapply(mylist,length) # Make vector with list lengths > > last_only <- mylist[[1:length(mylist)]][last] # Crash and burn trying to > make new vector with last items! > > How do I do this last step?See ?tail> lapply(mylist, tail, 1)[[1]] [1] 3 [[2]] [1] "dog" [[3]] [1] "zz" You can't actually create a vector, since your list contains both numeric and alpha data types and a vector can only contain a single data type. The 3 would be coerced to "3" (a character 3, not the number 3). If your actual data contains the same type in each element, replace lapply() above with sapply() and that will return a vector. HTH, Marc Schwartz
On May 25, 2011, at 3:25 PM, Seth W Bigelow wrote:> I have a list that is made of lists of varying length. I wish to > create a > new vector that contains the last element of each list. So far I > have used > sapply to determine the length of each list, but I'm stymied at the > part > where I index the list to make a new vector containing only the last > item > of each list > > mylist = list(c(1,2,3),c("cat","dog"),c("x","y","z","zz")) # > Create > list > > last <- sapply(mylist,length) # Make vector with list lengths > > last_only <- mylist[[1:length(mylist)]][last] # Crash and burn > trying to > make new vector with last items!If you wanted to apply the successive values of last using "[" to successive values of mylist there is a list-ish method via mapply: > mapply("[", mylist, last) [1] "3" "dog" "zz" `mapply` is also the function underlying `Vectorise` -- David Winsemius, MD West Hartford, CT