简体   繁体   中英

How to access the same column for all data frames inside a list

I have an object like this:

d1 <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
d2 <- data.frame(y1=c(3,2,1),y2=c(6,5,4))
d3 <- data.frame(y1=c(7,8,9),y2=c(5,2,6))
my.list <- list(d1, d2, d3)
names(my.list) <- c("d1","d2","d3")

Is there a way to access the column y2 of all data frames inside the list once?

Something like this:

my.list[["d1"]]$y2

But this only works for one data frame at a time

Try

lapply(my.list, '[[', 'y2')

Or use sapply to get the output as a matrix

sapply(my.list, `[[`, 'y2')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM