Hi Rui, thanks very much for your help - i've finally cracked it... the correct
code was
iscores<-list()for (j in seq(1,166,11)){for (i in j:(j+10))
{iscores[[i]=(i-j)*data[[i]] }}
instead of iscores[[i-j+1]]=(i-j)*data[[i-j+1]]
I probably wasn't explaining myself very well if you're interested i've
used dput as you suggested on a reduced dataset with just 22 variables and 5
observations. Each variable is a mood score by activity going from 0 to 10 (v1
to v11) and I wanted to multiply each cell by the value of the corresponding
score (so v1*0, v2*1, ... , v11*10):
> dput(head(mtest, 20))structure(list(Mood0 = c(0, 4, 0, 0, 0), Mood1 = c(0, 8,
> 0, 0, 0), Mood2 = c(0, 13, 0, 0, 0), Mood3 = c(0, 19, 0, 4, 0), Mood4 = c(0,
> 16, 5, 16, 4), Mood5 = c(19, 27, 16, 37, 2), Mood6 = c(169, 31, 63, 36, 6),
> Mood7 = c(13, 34, 107, 47, 12), Mood8 = c(0, 26, 17, 45, 43), Mood9 = c(0,
> 24, 0, 27, 100), Mood10 = c(0, 13, 0, 9, 53), `Mood0 and Travelling` = c(0,
> 92, 0, 0, 0), `Mood1 and Travelling` = c(0, 184, 0, 0, 0), `Mood2 and
> Travelling` = c(0, 299, 0, 0, 0), `Mood3 and Travelling` = c(0, 437, 0, 92,
> 0), `Mood4 and Travelling` = c(0, 368, 50, 368, 84), `Mood5 and Travelling` =
> c(494, 621, 160, 851, 42), `Mood6 and Travelling` = c(4394, 713, 630, 828,
> 126), `Mood7 and Travelling` = c(338, 782, 1070, 1081, 252), `Mood8 and
> Travelling` = c(0, 598, 170, 1035, 903)), .Names = c("Mood0", "Mood1",
> "Mood2", "Mood3", "Mood4", "Mood5", "Mood6", "Mood7", "Mood8", "Mood9",
> "Mood10", "Mood0 and Travelling", "Mood1 and Travelling", "Mood2 and
> Travelling", "Mood3 an!
d Travelling", "Mood4 and Travelling", "Mood5 and Travelling", "Mood6 and
Travelling", "Mood7 and Travelling", "Mood8 and Travelling"))
Using the test code below, i was finally able to achieve on this reduced
dataset what i set out to do...:
> mitest<-list()#list of the i*moodi values for 2 activities> for (j in
> seq(1,22,11)) + {+ for (i in j:(j+10))+ {mitest[[i]]=(i-j)*mtest[[i]]+
> }+ }> mtest[[1]][1] 0 4 0 0 0> mitest[[1]][1] 0 0 0 0 0> mtest[[6]][1] 19 27
> 16 37 2> mitest[[6]][1] 95 135 80 185 10> mtest[[12]][1] 0 92 0 0 0>
> mitest[[12]][1] 0 0 0 0 0> mtest[[17]][1] 494 621 160 851 42>
> mitest[[17]][1] 2470 3105 800 4255 210
Thanks again!
Christel
> Date: Fri, 8 Feb 2013 17:54:15 +
> From: ruipbarra...@sapo.pt
> To: christellac...@hotmail.co.uk
> CC: r-help@r-project.org
> Subject: Re: [R] help with double looping
>
> Hello,
>
> You're right, sorry for the misleading tip. How about seq(1, 177, 11)?
> Please note that without a data example, it's not very easy to say.
> Can't you post a small dataset using ?dput
>
> dput(head(data, 20)) # paste the output of this.
>
>
> Rui Barradas
>
> Em 08-02-2013 17:10, christel lacaze escreveu:
> >
> > that doesn't seem to be the issue i'm afraid... both j sequences produce
> > the same numbers:
> >
> >> j<-seq(1,166,11)
> >> j
> > [1] 1 12 23 34 45 56 67 78 89 100 111 122 133 144 155 166
> >> j<-seq(1,176,11)
> >> j
> > [1] 1 12 23 34 45 56 67 78 89 100 111 122 133 144 155 166
> >
> >> Date: Fri, 8 Feb 2013 16:37:52 +
> >> From: ruipbarra...@sapo.pt
> >> To: christellac...@hotmail.co.uk
> >> CC: r-help@r-project.org
> >> Subject: Re: [R] help with double looping
> >>
> >> Hello,
> >>
> >> Maybe seq(1, 16*11, 11)? (16*11 is 176, not 166)
> >>
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >> Em 08-02-2013 16:03, christel lacaze escreveu:
> >>>
> >>> hi there,
> >>>
> >>> I have a dataframe in the shape vA1, vA2,..., vA11, vB1, vB2,...,
> >>> VB11,..., VP1, VP2,, VP11 (so 16 times a sequence of 11 variables)
> >>> I am trying to build a double loop so that i can apply the function
> >>> (i-1)*v(i) to the first 10 variables, then the same for the next 10
> >>> variables, etc... 16 times.
> >>>
> >>> I have tried the following with no luck:
> >>>
> >>> iscores<-list()
> >>> for (j in seq(1,166,11))
> >>> {
> >>> for (i in j:(j+10))
> >>> {iscores[[i-j+1]]=(i-j)*data[[i-j+1]]
> >>> }
> >>> }
> >>>
> >>> any suggestion...?
> >>>
> >>> many thanks,
> >>>
> >>> Christel
> >>>
> >>> [[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]]
__
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.