Dear Mr Winsemius,

thank you for your message. I have signed up with this mailing list to receive some serious help. 
Since it does not seem possible, I won't be bothering you anymore.

Sincerly yours,
Tamara Rathcke
________________________________________
From: David Winsemius [via R] [ml-node+s789695n3811079h33@n4.nabble.com]
Sent: 13 September 2011 21:20
To: Tamara Rathcke
Subject: Re: writing a loop using several list()-objects

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

______________________________________________
[hidden email]</user/SendEmail.jtp?type=node&node=3811079&i=0> 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.


________________________________
If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/writing-a-loop-using-several-list-objects-tp3810215p3811079.html
To unsubscribe from writing a loop using several list()-objects, click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3810215&code=dGFtYXJhLnJhdGhja2VAZ2xhc2dvdy5hYy51a3wzODEwMjE1fDUwNzgxOTE4OA==>.


--
View this message in context: http://r.789695.n4.nabble.com/writing-a-loop-using-several-list-objects-tp3810215p3812274.html
Sent from the R help mailing list archive at Nabble.com.
	[[alternative HTML version deleted]]

