选择嵌入列表中的数据框中的特定列

时间:2016-07-08 14:12:28

标签: r for-loop dataframe subset

这是我目前的问题。我有一个由不同值组成的数据框列表。我希望能够遍历数据框列表,并根据我指定的列的名称为每个数据框选择特定的数据列。我想在一个单独的数据框列表中分配这些选定的列。

我使用了另一个列表对象,其中包含我想要提取的不同列的名称。

我已经采取了一些方法,但我仍处于头痛阶段。帮助将不胜感激!

以下是我在下面编写的示例代码:

# Create sample data set of five data frames, 10 x 10 

M1 <- data.frame(matrix(rnorm(5), nrow = 10, ncol = 10))
M2 <- data.frame(matrix(rnorm(10), nrow = 10, ncol = 10))
M3 <- data.frame(matrix(rnorm(15), nrow = 10, ncol = 10))
M4 <- data.frame(matrix(rnorm(20), nrow = 10, ncol = 10))
M5 <- data.frame(matrix(rnorm(25), nrow = 10, ncol = 10))

# Assign data frames to a list object

mlist<-list(M1, M2, M3, M4, M5)

# Creates a data frame object consisting of the different column names I want to extract later

df.names <- data.frame(One = c("X1", "X3", "X5"), Two = c("X2", "X4", "X6"))

# Converts df.names into a set of characters (not sure if this is needed but it has worked for me in the past)

df.char <- lapply(df.names, function(x) as.character(x[1:length(x)]))

# Creates variable m that will be used to iterate in the for loops below

m<-1:length(mlist)



# Creates list object to set aside selected columns from df.names   

mlist.selected<-list()

# A for loop to iterate for each of the df.names elements, and for each dataframe in mlist. *Hopefully* select out the columns of interest labeled in df.names, place into another list object for safe keeping
for (i in 1:length(df.names)) 
        {
        for(j in m)
                {
                #T his is the line of code I'm struggling with and I know it doesn't work. :-(
                mlist.selected[j]<-lapply(mlist, function(x) x[df.char[[i]]])

        }
}

1 个答案:

答案 0 :(得分:1)

使用

mlist.selected[[j]] <- lapply(mlist, function(x) x[df.char[[i]]])
你的for循环中的

会让你更接近。我建议使用带有

的命名列表
mlist.selected[[paste("m",j, names(df.names)[i], sep=".")]] <- 
                                                   lapply(mlist, function(x) x[df.char[[i]]])

获得更好的输出。

在检查时,这将返回重复的列表,我认为你不想要。如果我理解你想要做什么,你实际上可以摆脱内部(j)循环:

# create named list of the data.frames
mlist<-list("M1"=M1, "M2"=M2, "M3"=M3, "M4"=M4, "M5"=M5)

# run the loop
for (i in 1:length(df.names)) {
    mlist.selected[[paste(names(df.names)[i], sep=".")]] <-
                                                lapply(mlist, function(x) x[df.char[[i]]])
}

返回一个名称很好的列表。例如,您可以使用df.names$Twomlist.selected$Two$M2中访问M2中保存的矢量数据。

相关问题