[R] building a list in a loop

carol white wht_crl at yahoo.com
Fri Jun 5 10:40:04 CEST 2015


It might be an easy question but how to construct correctly a list in a loop? 

The following doesn't work
before starting the loopd = NULL#in the loop, 1st iteration
d = list(d,c(1,2,3)d[[1]]
NULL

[[2]]
[1] 1 2 3#in the loop, 2nd iterationd=list(d,c(4,5,6)d
[[1]]
[[1]][[1]]
NULL

[[1]][[2]]
[1] 1 2 3


[[2]]
[1] 4 5 6
the goal is to have the result of d= list(c(1,2,3),c(4,5,6)) where the list components are not known out of the loop.
d[[1]]
[1] 1 2 3

[[2]]
[1] 4 5 6
Moreover, how to name the components of the list in the loop while constructing as the names are not known out of the loop, either? note that the name of the component is stored in a variable in the loop

d = NULL#name1 contains the name for c(1,2,3), how to give the name below?
d = list(d,c(1,2,3)
Thanks
 
	[[alternative HTML version deleted]]



More information about the R-help mailing list