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.mcquil...@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]] ______________________________________________ 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.