使用lapply子集多个数据表 [英] subset multiple data tables using lapply

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

问题描述

我有多个数据表,并且所有数据表都有一个称为 ID 的公共列.我有一个向量 vec ,其中包含一组 ID 值.我想使用 lapply 使用 vec 子集所有数据表我了解如何使用 lapply 来对数据表进行子集化,但是我的问题是如何将子集化的结果分配回原始数据表中这是我尝试过的:

I have multiple data tables and all have a common column called ID. I have a vector vec that contains a set of ID values. I would like to use lapply to subset all data tables using vec I understand how to use lapply to subset the data tables but my question is how to assign the subsetted results back to original data tables Here is what I tried :

 tables<-c("dt1","dt2","dt3","dt4")
 lapply(mget(tables),function(x)x[ID %in% vec,])

上面给出了所有数据表的子集,但是我如何将它们分配回 dt1,dt2,dt3,dt4 ?

The above gives subsets of all data tables but how do I assign them back to dt1,dt2,dt3,dt4 ?

推荐答案

我将数据集保留在列表中,而不是在全局环境中更新数据集对象,因为大多数操作都可以在列表中完成(包括读取文件并写入输出文件).但是,如果您坚持要使用,我们可以使用 list2env ,它将使用子集数据集更新原始数据集对象

I would keep the datasets in the list rather than updating the dataset objects in the global environment as most of the operations can be done within the list (including reading the files and writing to output files ). But, if you insist, we can use list2env which will update the original dataset objects with the subset dataset

 lst <- lapply(mget(tables),function(x)x[ID %in% vec,])
 list2env(lst, envir=.GlobalEnv)

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

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