On Sep 13, 2011, at 10:26 AM, travellina wrote:

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

If you post the results of str() on all of those we would not face the barrier of (re-)creating them ourselves. I happen to find it a pain to have poster expect that we will edit all of their console output to get back to a reproducible state.


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]]

Are we supposed to have intuited that "sums" was the same as "durF"?

[[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.

Probably your use of "[" rather than "[[". (Untested hunch.)


I
am not ambitious about creating elegant loops,

You aren't too ambitious about reading the Posting Guide. either.

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

--


David Winsemius, MD
West Hartford, CT

______________________________________________
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.

Reply via email to