简体   繁体   中英

R How do I reference a colunm within a list of data.frames? And then use lapply?

Say I had a list of dataframes:

DF1 <- data.frame(mtcars)
DF2 <- data.frame(mtcars*2)
mydatalist <-list(DF1,DF2)

How would I apply a function on all the columns in a list, and return a list of the results? For example, say I want to extract the first number of the qsec column (the 7th one). I can easily call it up for the first item:

mydatalist[[1]][7]

But say I wanted to look at all of column 7 for each element in the list (both DF1 and DF2). How would I call that?

This doesn't work:

 mydatalist[[]][7]
 Error in mydatalist[[]] : invalid subscript type 'symbol'

My purpose is that I want to apply a function to that, say substr:

lapply(mydatalist[[1]][7],substr,0,2)
lapply(mydatalist[[2]][7],substr,0,2)

But this doesn't work:

lapply(mydatalist[[]][7],substr,0,2)

How do I do it?

Thanks!

To get only the 7th column of each data frame in the list, you can use

lapply(mydatalist, "[", 7)  ## you can also get multiple columns with c(1,5,etc)

for the column as a data frame, and

lapply(mydatalist, "[[", 7)

to view it as a vector. To reference it/them by name, you can use the name just like you would the column number

lapply(mydatalist, "[", "qsec")

To use substr on the column, first you have to access the column. Then you apply can use an anonymous function to do the work with substr .

lapply(mydatalist, function(x) substr(x[[7]], 0, 2)) ## or x[["qsec"]]
# [[1]]
#  [1] "16" "17" "18" "19" "17" "20" "15" "20" "22" "18"
# [11] "18" "17" "17" "18" "17" "17" "17" "19" "18" "19"
# [21] "20" "16" "17" "15" "17" "18" "16" "16" "14" "15"
# [31] "14" "18"
# 
# [[2]]
#  [1] "32" "34" "37" "38" "34" "40" "31" "40" "45" "36"
# [11] "37" "34" "35" "36" "35" "35" "34" "38" "37" "39"
# [21] "40" "33" "34" "30" "34" "37" "33" "33" "29" "31"
# [31] "29" "37"

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