Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a list in a loop in R - getting item names correct

Tags:

I have a function which contains a loop over two lists and builds up some calculated data. I would like to return these data as a lists of lists, indexed by some value, but I'm getting the assignment wrong.

A minimal example of what I'm trying to do, and where i'm going wrong would be:

mybiglist <- list() for(i in 1:5){     a <- runif(10)     b <- rnorm(16)     c <- rbinom(8, 5, i/10)     name <- paste('item:',i,sep='')     tmp <- list(uniform=a, normal=b, binomial=c)     mybiglist[[name]] <- append(mybiglist, tmp) } 

If you run this and look at the output mybiglist, you will see that something is going very wrong in the way each item is being named.

Any ideas on how I might achieve what I actually want?

Thanks

ps. I know that in R there is a sense in which one has failed if one has to resort to loops, but in this case I do feel justified ;-)

like image 404
Hassantm Avatar asked Sep 20 '12 11:09

Hassantm


People also ask

Can you name items in a list R?

The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to access them. Named list can also be created using names() function to specify the names of elements after defining the list.

Can we use for loop for list?

Using Python for loop to iterate over a list. In this syntax, the for loop statement assigns an individual element of the list to the item variable in each iteration. Inside the body of the loop, you can manipulate each list element individually.

How do you make a list of lists in R?

How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.


1 Answers

It works if you don't use the append command:

mybiglist <- list() for(i in 1:5){   a <- runif(10)   b <- rnorm(16)   c <- rbinom(8, 5, i/10)   name <- paste('item:',i,sep='')   tmp <- list(uniform=a, normal=b, binomial=c)   mybiglist[[name]] <- tmp }  # List of 5 # $ item:1:List of 3 # ..$ uniform : num [1:10] 0.737 0.987 0.577 0.814 0.452 ... # ..$ normal  : num [1:16] -0.403 -0.104 2.147 0.32 1.713 ... # ..$ binomial: num [1:8] 0 0 0 0 1 0 0 1 # $ item:2:List of 3 # ..$ uniform : num [1:10] 0.61 0.62 0.49 0.217 0.862 ... # ..$ normal  : num [1:16] 0.945 -0.154 -0.5 -0.729 -0.547 ... # ..$ binomial: num [1:8] 1 2 2 0 2 1 0 2 # $ item:3:List of 3 # ..$ uniform : num [1:10] 0.66 0.094 0.432 0.634 0.949 ... # ..$ normal  : num [1:16] -0.607 0.274 -1.455 0.828 -0.73 ... # ..$ binomial: num [1:8] 2 2 3 1 1 1 2 0 # $ item:4:List of 3 # ..$ uniform : num [1:10] 0.455 0.442 0.149 0.745 0.24 ... # ..$ normal  : num [1:16] 0.0994 -0.5332 -0.8131 -1.1847 -0.8032 ... # ..$ binomial: num [1:8] 2 3 1 1 2 2 2 1 # $ item:5:List of 3 # ..$ uniform : num [1:10] 0.816 0.279 0.583 0.179 0.321 ... # ..$ normal  : num [1:16] -0.036 1.137 0.178 0.29 1.266 ... # ..$ binomial: num [1:8] 3 4 3 4 4 2 2 3 
like image 138
Sven Hohenstein Avatar answered Sep 20 '22 05:09

Sven Hohenstein