使用动态创建的字符对象引用现有数据框 [英] Referencing an existing data frame with a dynamically-created character object

查看:85
本文介绍了使用动态创建的字符对象引用现有数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该有一个简单的解决方案,但我无法理解.

This feels like this should have a simple solution but it eludes me.

我有一组现有的数据帧,分别称为set1,set2和set3.我想将它们中的每一个(分别)与另一个数据框合并,如下所示:

I have an existing set of data frames, called set1, set2, and set3. I want to merge each of them - separately - with another data frame, like so:

a <- merge(bigdata, set1, by = keyID)
b <- merge(bigdata, set2, by = keyID)
c <- merge(bigdata, set3, by = keyID)
...

我想动态地(循环地)引用现有的集合",所以我创建了这个对象:

I want to reference the existing 'sets' on the fly (in a loop), so I made this object:

nam <- paste('set', i, sep = '')

当然,如果我这样做:

> nam

我明白了:

> "set1"

但是我想使用"set1"引用来表示合并代码中的适当数据帧,而不仅仅是"chr"类的对象.一如既往,我们将不胜感激.

But I want to use that "set1" reference to represent the apprpopriate data frame up in the merging code, not just an object of class "chr." Any help is, as always, appreciated.

推荐答案

您可以按照Juba的建议使用get,也可以创建输入列表.

You could use get as Juba suggested, or you make an input list.

 bigset<-list(set1=set1,set2=set2, [etc)
 abc_list <- sapply(1:length(bigset) function(j) merge(bigdata,bigset[[j]])

您就完成了. 或者,不创建bigset

and you're done. Alternatively, without creating bigset,

setnames<-ls(pattern='set[0-9]{1,})
abc_list<-sapply(1:length(setnames), function(j) merge(bigdata,get(setnames[j]))

这篇关于使用动态创建的字符对象引用现有数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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