2010-08-08 61 views
1

好吧,這真的很基本。如何將元素添加到R中的(指定)列表?推送到R列表

編輯 當鍵名是varibale

for (name in names(list$filenames)) { 
    filename <- list$filenames[[name]] 
    x <- read.table(filename) 
    ret$name <- x # I want name to be interpreted here, not use "name" 
} 
+0

[R:如何將可變鍵/值對添加到列表對象?](http://stackoverflow.com/questions/1105659/r-how-to-add-variable-key-value-pair對於列表對象) – hadley 2010-08-08 14:01:37

回答

2

也許ret<-lapply(list$filenames,read.table)會更好?

+1

也許在這種情況下,它會,但我很樂意知道熱的一般做它。 – 2010-08-08 10:57:00

+0

一般來說,ret [[name]] < - x – 2010-08-08 11:01:19

+1

+1謝謝,'[[...]]'是我一直在尋找的! – 2010-08-08 11:02:19

0

回覆:

ret$name <- x # I want name to be interpreted here, not use "name" 

這是同樣的事情,因爲這:

ret[["name"]] 

而是執行此操作:

ret[name] 

或者,如果你想要一個簡單的載體背面,而不是什麼作爲與ret類型相同,請執行以下操作:

ret[[name]]