Keshav M
Keshav M

Reputation: 1349

How to extract a specific column from a list of data frames into a new list of data frames?

How do I extract the second column of each data frame in this list, and store it as another new list of single column data frames? Leaving it as a data frame is very important in the future use of this list.

c1 <- runif(10, 0, 10)
c2 <- runif(10, 0, 10)
c3 <- runif(10, 0, 10)
c4 <- runif(10, 0, 10)
c5 <- runif(12, 0, 10)
c6 <- runif(12, 0, 10)
c7 <- runif(12, 0, 10)
c8 <- runif(12, 0, 10)


df1 <- data.frame(c1, c2, c3, c4)
df2 <- data.frame(c5, c6, c7, c8)

list <- list(df1, df2)

Upvotes: 0

Views: 46

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

You can do:

lapply(list, "[", 2)

Upvotes: 2

Related Questions