Dimitri Liakhovitski
2013-Feb-03 17:33 UTC
[R] Looping through rows of all elements of a list that has variable length
Dear R-ers, I have a list of data frames such that the length of the list is unknown in advance (it could be 1 or 2 or more). Each element of the list contains a data frame. I need to loop through all rows of the list element 1 AND (if applicable) of the list element 2 etc. and do something at each iteration. I am trying to figure out how to write a code that is generic, i.e., loops through the rows of all elements of my lists even if the total number of the list elments is unknown in advance. Below is an example. a=expand.grid(1:2,1:2) b=expand.grid(1:2,1:2,1:2) ################################################# # My list that can have 1 element, e.g.: l.short<-vector("list",1) l.short[[1]]<-a # I need to loop through rows of l.short[[1]] and do somethinig (it's unimportant what exactly) with them, e.g.: out<-vector("list",nrow(l.short[[1]])) for(i in 1:nrow(l.short[[1]])){ # i<-1 out[[i]]<-sum(l.short[[1]][i,]) } (out) ################################################# # Or my list could have >1 elements, e.g., 2 like below (or 3 or more). # The total length of my list varies. l.long<-list(a,b) # I need to loop through rows of l.long[[1]] AND of l.long[[2]] simultaneously # and do something with both, - see example below. # Below, I am doing it "manually" by using expand.grid to create all combinations of rows of 2 elements of 'l.long': mygrid<-expand.grid(1:nrow(l.long[[1]]),1:nrow(l.long[[2]])) out<-vector("list",nrow(mygrid)) for(gridrow in 1:nrow(mygrid)){ # gridrow<-1 row.a<-mygrid[gridrow,1] row.b<-mygrid[gridrow,2] out[[gridrow]]<-sum(l.long[[1]][row.a,])+sum(l.long[[2]][row.b,]) } Thank you very much for any suggestions! -- Dimitri Liakhovitski gfk.com <http://marketfusionanalytics.com/> [[alternative HTML version deleted]]
nalluri pratap
2013-Feb-04 14:43 UTC
[R] Looping through rows of all elements of a list that has variable length
Just modified your code a bit, hope this helps: a=expand.grid(1:2,1:2) b=expand.grid(1:2,1:2,1:2) c=expand.grid(1:2,1:2,1:2,1:2) l.long<-list(a,b,c) mygrid<-do.call(expand.grid,lapply(l.long,function(x) 1:nrow(x))) out<-vector("list",nrow(mygrid)) for(gridrow in 1:nrow(mygrid)) { sum_rows=0 for (i in seq_along(mygrid)) { myrow<-mygrid[gridrow,i] sum_rows=sum_rows+sum(l.long[[i]][myrow,]) } out[[gridrow]]=sum_rows } Pratap --- On Sun, 3/2/13, Dimitri Liakhovitski <dimitri.liakhovitski@gmail.com> wrote: From: Dimitri Liakhovitski <dimitri.liakhovitski@gmail.com> Subject: [R] Looping through rows of all elements of a list that has variable length To: "r-help" <r-help@r-project.org> Date: Sunday, 3 February, 2013, 11:03 PM Dear R-ers, I have a list of data frames such that the length of the list is unknown in advance (it could be 1 or 2 or more). Each element of the list contains a data frame. I need to loop through all rows of the list element 1 AND (if applicable) of the list element 2 etc. and do something at each iteration. I am trying to figure out how to write a code that is generic, i.e., loops through the rows of all elements of my lists even if the total number of the list elments is unknown in advance. Below is an example. a=expand.grid(1:2,1:2) b=expand.grid(1:2,1:2,1:2) ################################################# # My list that can have 1 element, e.g.: l.short<-vector("list",1) l.short[[1]]<-a # I need to loop through rows of l.short[[1]] and do somethinig (it's unimportant what exactly) with them, e.g.: out<-vector("list",nrow(l.short[[1]])) for(i in 1:nrow(l.short[[1]])){ # i<-1 out[[i]]<-sum(l.short[[1]][i,]) } (out) ################################################# # Or my list could have >1 elements, e.g., 2 like below (or 3 or more). # The total length of my list varies. l.long<-list(a,b) # I need to loop through rows of l.long[[1]] AND of l.long[[2]] simultaneously # and do something with both, - see example below. # Below, I am doing it "manually" by using expand.grid to create all combinations of rows of 2 elements of 'l.long': mygrid<-expand.grid(1:nrow(l.long[[1]]),1:nrow(l.long[[2]])) out<-vector("list",nrow(mygrid)) for(gridrow in 1:nrow(mygrid)){ # gridrow<-1 row.a<-mygrid[gridrow,1] row.b<-mygrid[gridrow,2] out[[gridrow]]<-sum(l.long[[1]][row.a,])+sum(l.long[[2]][row.b,]) } [[elided Yahoo spam]] -- Dimitri Liakhovitski gfk.com <http://marketfusionanalytics.com/> [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Seemingly Similar Threads
- expand.grid on contents of a list
- identifying weeks (dates) that certain days (dates) fall into
- Fastest way to compare a single value with all values in one column of a data frame
- grabbing from elements of a list without a loop
- Select only unique rows from a data frame