R:如何在循环内绑定嵌套循环的所有数据帧的特定列?

R: How can I cbind specific columns of all data frames of a nested loop within the loop?

我正在尝试在同一个循环过程中合并多个数据框的第三列,这些数据框在嵌套 for 循环中被调用和重命名。

# Sample Data
ecvec_msa6_1998=matrix( round(rnorm(200, 5,15)), ncol=4)
ecvec_msa6_1999=matrix( round(rnorm(200, 4,16)), ncol=4)
ecvec_msa6_2000=matrix( round(rnorm(200, 3,17)), ncol=4)

datasets=c("msa")
num_industrys=c(6)
years=c(1998, 1999, 2000)

alist=list() 

for (d in 1:length(datasets)) {
  dataset=datasets[d]
  for (n in 1:length(num_industrys)){
    num_industry=num_industrys[n]
    for (y in 1:length(years)) {
      year=years[y]

     eval(parse(text=paste0("newly_added = ecvec_", dataset, num_industry, "_",  year))) 
     # renaming the old data frames

     alist = list(alist, newly_added) # combining them in a list

     extracted_cols <- lapply(alist, function(x) x[3]) # selecting the third column

     result <- do.call("cbind", extracted_cols) # trying to cbind the third colum

    }
  }
}

有人可以告诉我正确的方法吗?

您是否只是想从每个数据框中提取第三列并将其组合成一个新列?

newdata <- cbind(ecvec_msa6_1998[,3],ecvec_msa6_1999[,3],ecvec_msa6_2000[,3])

通常建议避免 R 中的嵌套循环:

See Circle 2 of R's Inferno or here.

也许你应该尝试更换这部分

     extracted_cols <- lapply(alist, function(x) x[3]) # selecting the third column

     result <- do.call("cbind", extracted_cols) # trying to cbind the third colum

帕特里克·伯恩斯 (Patrick Burns) 在第一个 link(第 14 页)中完成了这样的列表。它也可以更干净。

您的代码几乎可以正常工作 - 这里有一些更改...

alist=list() 

for (d in 1:length(datasets)) {
  dataset=datasets[d]
  for (n in 1:length(num_industrys)){
    num_industry=num_industrys[n]
    for (y in 1:length(years)) {
      year=years[y]
      eval(parse(text=paste0("newly_added = ecvec_", dataset, num_industry, "_",  year)))                                   
      #the next line produces the sort of list you want - yours was too nested
      alist = c(alist, list(newly_added))
    }
  }
}

#once you have your list, these commands should be outside the loop          
extracted_cols <- lapply(alist, function(x) x[,3]) #note the added comma!
result <- do.call(cbind, extracted_cols) #no quotes needed around cbind

head(result)
     [,1] [,2] [,3]
[1,]   11   13   24
[2,]  -26   -3    7
[3,]   -1  -26  -14
[4,]    5   14  -15
[5,]   28    3    8
[6,]    9   -9   19

然而 - 一种更像 R(且更快)的方法是将上述所有内容替换为

df <- expand.grid(datasets,num_industrys,years) #generate all combinations
datanames <- paste0("ecvec_",df$Var1,df$Var2,"_",df$Var3) #paste them into a vector of names
result <- sapply(datanames,function(x) get(x)[,3])

sapply 如果可以的话,自动将列表简化为数据框(lapply 总是生成一个列表)