2015-09-26 113 views
2

比方說,我有這3個對象轉換字符串數組爲對象的數組中的R

first<-1 
second<-"hello" 
third<-3 

而且我有個字符的一個這樣的數組:

example<-c("first","second","third") 

我如何可以自動將所有數組中的名稱與具有相同名稱的對象?爲了有類似:

example2<-c(first, second, third) 

我知道有類似的問題,但我找不到任何答案,我可以做的工作我的情況了。 謝謝誰可以幫助

UPDATE:

如果我做的:然後

example3<-mget(example) 

螞蟻在R控制檯類型example2example3,輸出略有不同。

> example2 
[1] "1"  "hello" "3" 

> example3 
$first 
[1] 1 

$second 
[1] "hello" 

$third 
[1] 3 

我需要向示例3完全相同對象作爲示例2,因爲如果我跑cbind(example3)我得到一些錯誤,而cbind(example2)工作正常,但對象的數組是一樣的。 當然,在我的代碼中,名爲first,second和third的對象與本文中的類型不同,但邏輯相同。

+1

歡迎SO! 'sapply(例如,得到)'你在做什麼? – tonytonov

+5

'example = mget(...)'。 –

+0

謝謝@tonytonov和@konrad。我認爲用這兩個命令我不會像** example2 **那樣獲得相同的對象數組。實際上R中的example2輸出爲 '> example2 [1]「1」「hello」「3」' while example3 ** output after running'example3 = mget(example)'is: '>>示例3 $第一 [1] 1 $第二 [1] 「你好」 $第三 [1] 3' 並且這導致與cbind問題。 'cbind(example2)'工作,而'cbind(example3)'我得到錯誤_(列表)對象不能被強制鍵入'double'_。 當然_first_,_second_和_third_對象是不同的我的真實代碼 – gabriele

回答

1

從你最近的評論,康拉德·魯道夫的建議,我相信你正在尋找

unlist(mget(example), use.names = FALSE) 
# [1] "1"  "hello" "3"  
+0

謝謝你@richard,工作 – gabriele

相關問題