[R] Breaking up a vector
jim holtman
jholtman at gmail.com
Fri May 25 18:16:25 CEST 2012
Learn how to put parentheses in expression when you do not know what
the operator precedence is:
z <- lx[(1+(i-1)*7):((i)*7)]
On Fri, May 25, 2012 at 11:29 AM, AOLeary <aodhanol at gmail.com> 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-tp4631329.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.
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list