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) #as this is the length of the vector I know I will have s<-as.vector(s) s[0]<-0 for (i in 1:length(lambs)) # where lambs is a vector of length 207 consisting of temperature values { s[i]<-s[i-1]-mean(lambs) } I continually get the error message: Error in s[i] <- s[i - 1] - mean(lambs) : replacement has length zero When I merely use s[i]<-i-mean(lambs) it works so there is obviously something wrong with the s[i-1] but i cant see what. All I want is for each S[i] to be the previous value for S - the mean. Also, If I want to add a data point into the loop ie: s[i]<-s[i-1]-(X[i]+mean(lambs)) where X[i] is the data point corresponding to the ith value how do I do that as I dont have them defined as anything. I merely have lambs 31.3 12.3 232.2 . . . . . . etc for 207 values -- View this message in context: http://www.nabble.com/Sequences-tp22927714p22927714.html Sent from the R help mailing list archive at Nabble.com.
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, 2009, at 8:13 AM, 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. > > #To calculate the culmulative sums: > > s<-rep(0,207) #as this is the length > of the > vector I know I will have > s<-as.vector(s) > s[0]<-0 > for (i in 1:length(lambs)) # where lambs is a > vector of > length 207 consisting of temperature > > values > > > { > s[i]<-s[i-1]-mean(lambs) > } > > I continually get the error message: > > Error in s[i] <- s[i - 1] - mean(lambs) : replacement has length zero > > > When I merely use s[i]<-i-mean(lambs) it works so there is obviously > something wrong with the s[i-1] but i cant see what. All I want is > for each > S[i] to be the previous value for S - the mean! > -- > View this message in context: http://www.nabble.com/Sequences-tp22927714p22927714.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
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 performance (and style ;)) aspects. The main question remains: What do you want to calculate?! When correcting your code it actually returns -(1:207)*mean(lambs). Is this intended? You may look at ?cumsum for cumulative sums. hth. Melissa2k9 schrieb:> 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) #as this is the length of the > vector I know I will have > s<-as.vector(s) > s[0]<-0 > for (i in 1:length(lambs)) # where lambs is a vector of > length 207 consisting of temperature > values > > > { > s[i]<-s[i-1]-mean(lambs) > } > > I continually get the error message: > > Error in s[i] <- s[i - 1] - mean(lambs) : replacement has length zero > > > When I merely use s[i]<-i-mean(lambs) it works so there is obviously > something wrong with the s[i-1] but i cant see what. All I want is for each > S[i] to be the previous value for S - the mean! >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790
On Tue, Apr 7, 2009 at 8:13 AM, Melissa2k9 <m.mcquillan@lancaster.ac.uk>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 allows positive integer subscripts, and defines s[0] as the empty vector. You might think that assigning to an empty vector would give an error, but R semantics say instead that this is a no-op. This is so that things like s[ <<some boolean condition>> ] <- value will assign the value to all elements of s meeting the condition -- while allowing for the possibility that none of them meet that condition. If you try to assign to an illegal individual element, however, you do get an error: s[[0]] <- 45 # gives an error R's subscripting semantics are very convenient for many operations, but do take some getting used to. -s [[alternative HTML version deleted]]
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, 2009 at 7:13 AM, Melissa2k9 <m.mcquillan@lancaster.ac.uk>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. > > #To calculate the culmulative sums: > > s<-rep(0,207) #as this is the length of the > vector I know I will have > s<-as.vector(s) > s[0]<-0 > for (i in 1:length(lambs)) # where lambs is a vector of > length 207 consisting of temperature > values > > > { > s[i]<-s[i-1]-mean(lambs) > } > > I continually get the error message: > > Error in s[i] <- s[i - 1] - mean(lambs) : replacement has length zero > > > When I merely use s[i]<-i-mean(lambs) it works so there is obviously > something wrong with the s[i-1] but i cant see what. All I want is for each > S[i] to be the previous value for S - the mean! > -- > View this message in context: > http://www.nabble.com/Sequences-tp22927714p22927714.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. >[[alternative HTML version deleted]]
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. > > #To calculate the culmulative sums: > > s<-rep(0,207) #as this is the length of the > vector I know I will have > s<-as.vector(s) > s[0]<-0 > for (i in 1:length(lambs)) # where lambs is a vector of > length 207 consisting of temperature > values > > > { > s[i]<-s[i-1]-mean(lambs) > } > > I continually get the error message: > > Error in s[i] <- s[i - 1] - mean(lambs) : replacement has length zero > > > When I merely use s[i]<-i-mean(lambs) it works so there is obviously > something wrong with the s[i-1] but i cant see what. All I want is for each > S[i] to be the previous value for S - the mean.Hi Melissa, I think this is a index problem ... your code is: s<-rep(0,207) s<-as.vector(s) s[0]<-0 for (i in 1:length(lambs)){ s[i]<-s[i-1]-mean(lambs) } But try this code: s<-rep(0,207) s<-as.vector(s) s[1]<-0 # not s[0] for (i in 2:length(lambs)){ #not 1:length(lambs) s[i]<-s[i-1]-mean(lambs) } The vector in R begin in 1 not in 0... -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil
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.mcquillan at lancaster.ac.uk schrieb:> Hi, > > I am trying to perform a change point analysis. I know there is a function > that does this but I am just out of university on an internship so they > are trying to make me do it myself using this website : > > http://www.variation.com/cpa/tech/changepoint.html > > So if you look for CUMSUM on this you will see what I am trying to do but > I'm not having much success! > > Melissa > > >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790