2016-03-07 48 views
1

我想將現有數據框重命名爲變量中的名稱。任何想法都表示讚賞。動態重命名數據幀

rtbl <- load("clarktestjunk.RData") 

datasetname = "specialdata" # passed in by the user 
rename(rtbl,datasetname) # this is not the correct command 

specialdata # this is how I want to reference the data set down stream. 
+0

相關文章:http://stackoverflow.com/questions/22951811,http://stackoverflow.com/questions/2717757 – zx8754

+0

我想通了。 eval(parse(text = paste(datasetname,「< - rtbl」))) –

+0

「如果答案是parse(),你通常應該重新考慮這個問題。」 - Thomas Lumley R-help(2005年2月)。此外,請參閱[具體是什麼eval(parse(...))的危險?](http://stackoverflow.com/questions/13649979/what-specifically-are-the-dangers-of-evalparse) – zx8754

回答

1

這是一個辦法(這不僅對數據幀作品):

old.name=data.frame(a=1:5,b=6:10) 
assign("new.name",old.name) 

> new.name 
    a b 
1 1 6 
2 2 7 
3 3 8 
4 4 9 
5 5 10 
1

的對象不能在你的僞代碼暗示的方式進行重命名。您可以將data.frame的內容分配給具有已知名稱的新對象。 (請參閱@ Wave的解決方案assign。)但原始對象仍然存在。

rm(list=ls()) 
data(cars) 
ls() 
# [1] "cars" 
assign("renamed_cars", cars) 
ls() 
# [1] "cars"   "renamed_cars" 
rm(cars) 
ls() 
# [1] "renamed_cars"