本文介绍了如何使用字符串变量为ddply创建变量列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用R的内置ToothGrowth示例数据集,此方法有效:
Using R's builtin ToothGrowth example dataset, this works:
ddply(ToothGrowth, .(supp,dose), function(df) mean(df$len))
但是我希望子集因子是变量,例如
But I would like to have the subsetting factors be variables, something like
factor1 = 'supp'
factor2 = 'dose'
ddply(ToothGrowth, .(factor1,factor2), function(df) mean(df$len))
那是行不通的.应该怎么做?
That doesn't work. How should this be done?
我想也许是这样的:
factorCombo = paste('.(',factor1,',',factor2,')', sep='')
ddply(ToothGrowth, factorCombo, function(df) mean(df$len))
但是它也不起作用.我想我很亲密,但不确定执行此操作的正确方法.我想可以将整个命令放入一个字符串中,然后再调用该字符串的eval(),但是希望有一种更优雅的方法吗?
But it doesn't work either. I think I am close, but not sure the proper way to do it. I suppose the entire command could be put into a string, followed by an eval() call of the string, but hopefully is there a more elegant way?
推荐答案
尝试:
x <- c("supp", "dose")
ddply(ToothGrowth, x, function(df) mean(df$len))
这篇关于如何使用字符串变量为ddply创建变量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!