Hello, Tip: see the difference between the following two.
for(i in 1:7) cat(i, ":", (i-1)*7:(i)*7, "\n") for(i in 1:7) cat(i, ":", ((i-1)*7):(i*7), "\n") (operator ':' has high precedence...) Hope this helps, Rui Barradas AOLeary wrote > > Hi all, > > My problem is as follows: > > I want to run a loop which calculates two values and stores them in > vectors r and rv, respectively. > They're calculated from some vector x with length a multiple of 7. > > x <- c(1:2058) > > I need to difference the values but it would be incorrect to difference it > all in x, it has to be broken up first. I've tried the following: > > r <- c(1:294)*0 > rv <- c(1:294)*0 > > #RUN A LOOP WHERE YOU INPUT THE lx[(i-1)*7:i*7] INTO Z > for (i in 1:294){ > #CREATE A NEW VECTOR OF LENGTH 7 > z <- NULL > length(z)=7 > dz <- NULL > dz2 <- NULL > > #STORE THE VALUES IN z > z <- lx[1+(i-1)*7:(i)*7] > > #THEN DIFFERENCE THOSE > #THIS IS r_t,i,m > dz=diff(z) > > #SUM THIS UP AND STORE IT IN r, THIS IS r_t > r[i] <- sum(dz) > > #SUM UP THE SQUARES AND STORE IT IN rv, THIS IS RV_t > dz2 <- dz^2 > rv[i] <- sum(dz2) > #END THE LOOP > } > > > However, the window seems to expand for some reason, so z ends up being a > much longer vector than it should be and full of NAs. > > > Any help or advice is much appreciated. > > Aodhán > -- View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-vector-tp4631329p4631342.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.