2016-07-07 16 views
3

我有我需要加載文件類型.Rdata對象的名稱未知的對象的問題。我可以在load函數中使用verbose = TRUE來獲取名稱。然後我用get重命名對象,以便將對象名稱標準化以用於進一步的處理步驟。現在我想通過調用這個名稱來刪除原始對象。我似乎無法得到rm解決我的對象內舉行的字符串,它記錄的名字:如何在對象名稱未知時動態刪除R中的加載對象?

a <- 1:10 # original data 'a' 

z <- tempfile() 
save(a, file=z) # saved to tempfile that doesn't contain the object name 
rm(a) # then removed 

obj.name <- load(z, verbose = TRUE) # 'a' is loaded and name recorded 
b <- get(obj.name) # object is passed to 2nd object 'b' to standardize name 

rm(list(get(obj.name))) # can't remove 'a' this way 
# Error in rm(list(get(obj.name))) : 
# ... must contain names or character strings 

file.remove(z) # cleanup 

回答

3

只需使用rm(list = obj.name)list接受字符。

+0

太容易了。謝謝! –

相關問題