【问题标题】:How to call different data frames in a loop?如何循环调用不同的数据帧?
【发布时间】:2017-02-09 14:04:57
【问题描述】:

我有 7 个数据框称为:

"muestra_suplentes_103"
"muestra suplentes_108"
"muestra_suplentes_109"
"muestra_suplentes_113"
"muestra_suplentes_114"
"muestra_suplentes_138"
"muestra suplentes_139"

我还有一个向量叫做:

cod_jer_groups= 103 108 109 113 114 138 139

我需要为此数据框执行此操作:

muestra_suplentes_103$nrow=seq(from = 1, to = nrow(muestra_suplentes_103), by = 1)
muestra_suplentes_108$nrow=seq(from = 1, to = nrow(muestra_suplentes_108), by = 1)
muestra_suplentes_109$nrow=seq(from = 1, to = nrow(muestra_suplentes_109), by = 1)
muestra_suplentes_113$nrow=seq(from = 1, to = nrow(muestra_suplentes_113), by = 1)
muestra_suplentes_114$nrow=seq(from = 1, to = nrow(muestra_suplentes_114), by = 1)
muestra_suplentes_138$nrow=seq(from = 1, to = nrow(muestra_suplentes_138), by = 1)
muestra_suplentes_139$nrow=seq(from = 1, to = nrow(muestra_suplentes_139), by = 1)

我打算这样做:

for(i in cod_jer_groups){
   muestra_suplentes$nrow= seq(from = 1, to = nrow(muestra_suplentes_i, by =  1))
   names(muestra_suplentes)[length(names(muestra_suplentes))]="nrow"
  }

我知道调用“muestra_suplentes_i”是错误的,但我不知道该怎么做。

有什么建议吗?

谢谢!

【问题讨论】:

  • 您可以使用get()set()。但是,您应该考虑使用list()

标签: r loops for-loop seq


【解决方案1】:

这就是我要做的。

# put the data.frames into a named list, where names correspond to data.frame names
myList <- mget(ls(pattern="^muestra_suplentes")

# fill in the column values
myList <- lapply(myList, function(i) within(i, rowNum <- seq_len(nrow(i))))

要详细了解为什么将 data.frames 放入行中可能是个好主意,请查看 gregor 对this post 的回答。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2017-10-27
    • 2013-07-12
    • 2018-06-09
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多