Hi there, I am looking for some help with a loop which uses three list()-objects: "sequence" has got numbers, "start" and "end" have got the begin/end time points which should be applied to the numbers in "sequence". length() of all three objects is identical, length() of vectors stored in "sequence" is longer than length() of "start" and "end" whose length() is identical. Here is an example:
> sequence[2] [[1]] [1] 168.848 188.440 87.291 111.333 159.921 76.987 65.633 293.320 440.708 > start[2] [[1]] [1] 2 5 8 > end[2] [[1]] [1] 4 7 9 I want to calculate sums of numbers in all start:end positions and list the output again, e.g. I can calculate the sum of the numbers occurring positions 2 till 4 of "sequence", i.e. between start[[2]][1] and end[[2]][1] using this code: sum(unlist(sequence[[2]])[unlist(Sfoot[[2]])[1]:unlist(end[[2]])[1]]) This code works perfectly fine but stops doing so if I put it into a loop: sums <- list() for(i in 1:length(start)) { for(j in 1:length(unlist(start[[i]]))) { sums[[i]][j] <- list(sum(unlist(sequence[[i]])[unlist(start[[i]])[j]:unlist(end[[i]])[j]])) }} The output looks like this: > durF[[2]] [[1]] [1] 387.064 [[2]] [1] 302.541 [[3]] [1] 734.028 instead of: > durF[[2]] [[1]] [1] 387.064 302.541 734.028 Something seems to be wrong with the indexing but no idea how to fix it. I am not ambitious about creating elegant loops, they simply should work - so please do not hesitate to make any suggestion: clunky or not, it will be greatly appreciated as I have been stuck with this loop for a week now! Many thanks, Tamara -- View this message in context: http://r.789695.n4.nabble.com/writing-a-loop-using-several-list-objects-tp3810215p3810215.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.