Hi Melissa,
so what you want to calculate is something like
S<-cumsum(lambs)-(1:length(lambs))*mean(lambs)
since (with x.bar=mean(x))
S_1=x_1-x.bar
S_2=S1+(x_2-x.bar)=x_1-x.bar+x_2-x_bar=x_1+x_2-2*x.bar
...
S_n=S_(n-1)+x_n-x.bar=sum_i (x_i)-n*x.bar
Eik.
m.mcquil...@lancaster.ac.uk schrieb:
Hi
On Tue, 2009-04-07 at 05:16 -0700, Melissa2k9 wrote:
> Hi,
>
> I am trying to make a sequence and am using a for loop for this. I want to
> start off with an initial value ie S[0]=0 then use the loop to create other
> values. This is what I have so far but I just keep getting error messages.
>
>
an R-centric in detail). I think it's fair to say that the man pages do
> not provide all of the details, either. As a result, I still occasionally
> get bitten by these indexing subtleties (which, of course, may just be due
> to my dummheit).
>
> Cheers,
> Bert
>
>
t).
Cheers,
Bert
Bert Gunter
Genentech Nonclinical Biostatistics
650-467-7374
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Stavros Macrakis
Sent: Tuesday, April 07, 2009 9:11 AM
To: Melissa2k9
Cc: r-help@r-project.org
Sub
Not sure what you're trying to accomplish, but I think the index values are
off. the first element of s is 1, not 0
Here's something that works:
s<-rep(0,207)
s<-as.vector(s)
s[0]<-0
lambs=rep(rnorm(207)*1000)
for (i in 1:(length(lambs)-1)){
s[i]<-s[i+1]-mean(lambs)
}
On Tue, Apr 7, 20
On Tue, Apr 7, 2009 at 8:13 AM, Melissa2k9 wrote:
>
> I am trying to make a sequence and am using a for loop for this. I want to
> start off with an initial value ie S[0]=0 then use the loop to create other
> values. This is what I have so far but I just keep getting error messages.
>
R only all
Hi Melissa,
first of all in R indexes of vectors starts with 1 not with 0. So
s[0]<-0 doesn't make sense here (minor second point R is also case
sensitive, so S[] is not the same as s[]).
Secondly rep() returns already a vector, so as.vector(s) is not necessary.
Thirdly, loops are "bad" under p
Think about what i-1 would be the first time though the loop. R
doesn't like 0 as an index. You are going to need to decide what you
consider to the the "previous value" for the first element in your
vector.
If you want cumulative sums, then look at the function cumsum
?cumsum
On Apr 7, 2
Hi,
I am trying to make a sequence and am using a for loop for this. I want to
start off with an initial value ie S[0]=0 then use the loop to create other
values. This is what I have so far but I just keep getting error messages.
#To calculate the culmulative sums:
s<-rep(0,207)
9 matches
Mail list logo