使用函数参数作为 R 中新数据框的名称 [英] Use function argument as name for new data frame in R
本文介绍了使用函数参数作为 R 中新数据框的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个很简单,但是我搜索了这个小问题没有找到解决方案.
this is very simple, but I have searched and failed to find a solution for this small problem.
我想使用函数的参数作为新数据框的名称,例如:
I want to use the argument of a function as the name for a new data frame, for example:
assign.dataset<-function(dataname){
x<-c(1,2,3)
y<-c(3,4,5)
dataname<<-rbind(x,y)
}
然后
assign.dataset(new.dataframe.name)
只需创建一个名为 dataname 的新数据集.
just creates a new dataset with the name dataname.
我曾尝试使用 paste 和 assign 功能,但没有成功.
I have tried to use the paste and assign functions but with no success.
非常感谢
推荐答案
你可以这样做...
assign.dataset<-function(dataname){
x<-c(1,2,3)
y<-c(3,4,5)
assign(deparse(substitute(dataname)), rbind(x,y), envir=.GlobalEnv)
}
assign.dataset(new.dataframe.name)
new.dataframe.name
[,1] [,2] [,3]
x 1 2 3
y 3 4 5
这篇关于使用函数参数作为 R 中新数据框的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文