[R] Creating a new vector ( another problem)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Nov 20 16:36:29 CET 2006


Amir Safari <amsa36060 at yahoo.com> writes:

>  Suppose we are  interested for generating a new vector ( x ) from a current vector (y)  of length 1000 so that x includes the sum of every 5 values in y  respectively from the first to the end of length y. The same length of  y for x is desired, so that other 4 positions (indices) in x are filled  out with NA.
>   
>   For generating such a new vector, I have no idea. I tried in some ways  but all were wrong and therefore I do not want to confuse you.
>   How could such a purpose be coded?
>   Please help me with any idea. Thank you so much for help.
>   Amir Safari

Here's one idea (for the case 1000 == 20 ...):

y <- rpois(20, 3)
s <- colSums(matrix(y,nrow=5))
r <- rep(NA, 20)
r[(1:20) %% 5 == 0] <- s

cbind(y,r)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list