Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

r - How to extract certain columns from a list of data frames

I have a list 'l' of data frames. These data frames in itself are 2-dimensional matrices. For my work, I'm required to create another list which has data frames which are a subset of the data frames from the original list.

Eg: List l1 has two data frames D1 and D2, having 10 and 12 different columns of data respectively. Now I want to create a new list l2 which also has two data frames but these data frames are columns picked out from the earlier data frames D1 and D2. Please consider that the position of the same column in D1 and D2 could be different, therefore I would have to access it through column name and not index

Could someone please suggest how I could go about implementing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an example (this is the kind of thing you should have put in your question. You will get near-instantaneous help if you can structure your question with a clear, copy/pasteable, reproducible example like this.)

Problem:

# list of data frames:
l = list(mtcars, mtcars)

# vector of column names I would like to extract
my_names = c("mpg", "wt", "am")
# these columns might be at different positions in the data frames

Solution:

result = lapply(l, "[", , my_names)

# look at the top 6 rows of each to verify that it worked:
lapply(result, head)
# [[1]]
#                    mpg    wt am
# Mazda RX4         21.0 2.620  1
# Mazda RX4 Wag     21.0 2.875  1
# Datsun 710        22.8 2.320  1
# Hornet 4 Drive    21.4 3.215  0
# Hornet Sportabout 18.7 3.440  0
# Valiant           18.1 3.460  0
#
# [[2]]
#                    mpg    wt am
# Mazda RX4         21.0 2.620  1
# Mazda RX4 Wag     21.0 2.875  1
# Datsun 710        22.8 2.320  1
# Hornet 4 Drive    21.4 3.215  0
# Hornet Sportabout 18.7 3.440  0
# Valiant           18.1 3.460  0

Explanation: You essentially want to do l[[1]][, my_names], l[[2]][, my_names], ... lapply applies a function to every list element. In this case, the function is [, which takes rows as its first argument (we leave it blank to indicate all rows), columns as its second argument (we give it my_names). It returns the results in a list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...