遍历数据表 [英] Iterate through data tables

查看:103
本文介绍了遍历数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张桌子

tbl.1 <- data.table("A" = runif(5), "B" = runif(5))
tbl.2 <- data.table("A" = runif(5), "B" = runif(5))
tbl.3 <- data.table("A" = runif(5), "B" = runif(5))

我想用诸如

for (i in 1:3) {
  # Open tbl.i
  # Do something
}

这怎么办?我可以将表放在列表中,然后遍历列表,这样可以正常工作.但是,由于各种原因,我试图将表保留为唯一对象. 谢谢.

How can this be done? I can put the tables on a list an iterate through the list which works OK. However, I am trying to keep the tables as unique objects for various reasons. Thanks.

推荐答案

如果您不想将data.tables保留在列表中.您可以在您的环境中引用它们.在此示例中,它是一个全局环境.如果您的data.tables将填充在其他软件包中,则您需要更改环境.

If you don't want to keep data.tables in a list. You can refer to them in your environment. In this example it is a global environment. If your data.tables will be populated inside some other package then you would need to change the environment.

library(data.table)
tbl.1 <- data.table("A" = runif(5), "B" = runif(5))
tbl.2 <- data.table("A" = runif(5), "B" = runif(5))
tbl.3 <- data.table("A" = runif(5), "B" = runif(5))
for (i in paste0("tbl.",1:3)) {
    # Open tbl.i: get
    # Do something: str
    str(get(i, envir = .GlobalEnv))
}

这篇关于遍历数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆