Reputation: 4864
I am trying to iterate through different datasets, extracting numbers from them. The name of each dataset is used as column of a list, the numbers should be added to the column. Just like in this example this does not work:
> liste<-list()
> for (i in c("di","mi","do")){print(i); liste$i<-c(liste$i,4)}
[1] "di"
[1] "mi"
[1] "do"
> liste
$i
[1] 4 4 4
Starting with an empty list, I want to add a column called "di", and add the value 4 (lateron more values) to that column. However, R doesn't use the variable, but calls all columns i
. How can I fix the problem?
Upvotes: 0
Views: 471
Reputation: 13310
Use [
instead of $
:
liste<-list()
for (i in c("di","mi","do")){print(i); liste[[i]]<-4}
liste
Upvotes: 1