R:生成变量名称,计算函数列表中的函数,并将这些值分配给循环中生成的变量名称
如果已经有答案,请原谅,但我无法从档案中弄清楚。
我通过 R 中的 for 循环生成了一系列非常相似的函数:
adoptint.fun=list()
for(i in 1:40) {
#function name for each column
func.name <- paste('adoptint',i,sep='')
#function
func = paste('function(yearenter, adoptyear, yearleave) {ifelse(is.na(yearenter) | yearenter >', i+1905, ' | is.na(adoptyear) | yearleave > ', i+1905, ', NA, ifelse(yearenter <= ', i+1905, ' & adoptyear <= ', i+1905, ', 1, 0))}', sep='')
adoptint.fun[[func.name]] = eval(parse(text=func))
}
我现在有兴趣应用此函数来为尚未在数据框中创建的变量生成值。我想使用循环或类似的方法来完成此操作,因为在 40 次迭代中,尽管具体值发生了变化,但过程是相同的。代码看起来像这样:
#generate variables that will be inserted into dataframe, dfanal.reshape
var_names <- paste("dfanal.reshape$adopt", 1:40, sep="")
#run function i to obtain values for variable i, which should be appended to dataframe
for(i in 1:40){
var_names[i] <- eval(parse(paste("adoptint.fun[[" ,i, "]](dfanal.reshape$intoobsyear,dfanal.reshape$adoptyear,dfanal.reshape$yearleave)", sep="")))
}
我已经在 var_names 段中使用了 mget,但这似乎不起作用,并且 eval 段也不起作用(即,不分配由函数确定的值(效果很好) 。
再次,如果这个问题已经得到解答,我们深表歉意,并提前感谢您的帮助
Please excuse me if there are already answers to this, but I can't quite figure it out from the archives.
I have generated a list of very similar functions via a for-loop in R:
adoptint.fun=list()
for(i in 1:40) {
#function name for each column
func.name <- paste('adoptint',i,sep='')
#function
func = paste('function(yearenter, adoptyear, yearleave) {ifelse(is.na(yearenter) | yearenter >', i+1905, ' | is.na(adoptyear) | yearleave > ', i+1905, ', NA, ifelse(yearenter <= ', i+1905, ' & adoptyear <= ', i+1905, ', 1, 0))}', sep='')
adoptint.fun[[func.name]] = eval(parse(text=func))
}
I am now interested in applying this function to generate values for variables that have yet to be created in the dataframe. I want to do this using a loop or similar since the process is identical, though the specific values change, over the 40 iterations. The code would look something like:
#generate variables that will be inserted into dataframe, dfanal.reshape
var_names <- paste("dfanal.reshape$adopt", 1:40, sep="")
#run function i to obtain values for variable i, which should be appended to dataframe
for(i in 1:40){
var_names[i] <- eval(parse(paste("adoptint.fun[[" ,i, "]](dfanal.reshape$intoobsyear,dfanal.reshape$adoptyear,dfanal.reshape$yearleave)", sep="")))
}
I have played around with mget for the var_names segment, but that doesn't seem to work and the eval segment is also not working (i.e., not assigning the values determined by the function (which works fine) to the appropriate dataframe column.
Again, apologies if this has already been answered and thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向您的函数添加一个额外的参数怎么样?
利用数据框是一种特殊类型的列表这一事实,这将使您可以更轻松地进行替换。我相信这是您最初的问题:
还请检查帮助页面
?which
和?Extract
现在没有可重现的示例(请参阅如何制作一个出色的 R 可重现示例?),很难猜测您想要做什么以及如何做这样比较经济。您仍然使用大量计算时间。以下函数可能会执行您想要的操作:
它允许您简单地执行操作
即可获得所需的结果。假定变量的名称为
yearenter
、adoptyear
和yearleave
。据我所知,您必须在函数中将yearenter
更改为intoobsyear
,但这只是一个细节。学习使用索引将为您省去很多挫败感。如果添加一个参数就可以了,请永远不要再创建 40 个相同的函数。
How about adding an extra argument to your function?
This would allow you to do the replacement quite a lot easier, using the fact that a dataframe is a special kind of list. That was your original problem I believe :
Check also the help pages
?which
and?Extract
Now without reproducible example (see How to make a great R reproducible example? ), it's hard to guess what you want to do and how to do this more economical. You're still using a lot of calculation time. The following function might do what you want :
which allows you to simply do
to get the desired result. This is given that the names of your variables are
yearenter
,adoptyear
andyearleave
. As far as I can see, you have to changeyearenter
tointoobsyear
in the function, but that's a detail.Learning to use indices will save you a lot of frustration. And please, never ever make 40 identical functions again if adding one argument will do.