r - 从数据框列表中的每个数据框中调用特定列

标签 r lapply

我喜欢报告数据框列表中每个数据框的特定列。有任何想法吗?这是我的代码:

# Create dissimilarity matrix
df.diss<-dist(t(df[,6:11]))

mds.table<-list() # empty list of values
for(i in 1:6){ # For Loop to iterate over a command in a function
  a<-mds(pk.diss,ndim=i, type="ratio", verbose=TRUE,itmax=1000)
  mds.table[[i]]<-a # Store output in empty list
}

现在这里是我遇到麻烦的地方。存储值后,我无法从列表中的每个数据框中调用特定列。

# This function should call every $stress column from each data frame.
lapply(mds.table, function(x){
  mds.table[[x]]$stress
  })

再次感谢!

最佳答案

你们很亲密:

set.seed(1)
l_df <- lapply(1:5, function(x){
  data.frame(a = sample(1:5,5), b = sample(1:5,5))
})

lapply(l_df, function(x){
  x[['a']]
})

[[1]]
[1] 2 5 4 3 1

[[2]]
[1] 2 1 3 4 5

[[3]]
[1] 5 1 2 4 3

[[4]]
[1] 3 5 2 1 4

[[5]]
[1] 5 3 4 2 1

关于r - 从数据框列表中的每个数据框中调用特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838683/

相关文章:

r - 将新变量添加到 data.frames 列表的每个元素

r - 将 lapply 结果转换为 r 中的单个数据帧

检索新添加的行的索引 - R 中的 for 循环

r - 如何在成对相关散点图中包含密度着色

r - 在 Rstudio 中使用 RcppArmadillo 制作 R 包遇到错误 "undefined reference to dsyev"

r - 如何使 ggplot2 在此图中排除零值?

r - 在 lapply 函数中访问和保留列表名称

mysql - 使用 R 在 MySQL 上的临时 JSON 变量中使用双引号

r - 将数据框加载到列表中

r - 如何使 rownames 第一列并将给定单元格替换为数据框列表中的值?