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@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

______________________________________________
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.

Reply via email to