Dear R Users, 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
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 SafariHere'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
try this: y <- rnorm(1000) ################# x <- rep(NA, 1000) x[seq(5, 1000, 5)] <- colSums(matrix(y, 5)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Amir Safari" <amsa36060 at yahoo.com> To: <R-help at stat.math.ethz.ch> Sent: Monday, November 20, 2006 3:42 PM Subject: [R] Creating a new vector ( another problem)> > > > > > Dear R Users, > > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
rollapply in zoo can do that. coredata extracts the data portion from the returned zoo object: y <- 1:20 # test data library(zoo) coredata(rollapply(zoo(y), 5, sum, by = 5, na.pad = TRUE, align = "r")) On 11/20/06, Amir Safari <amsa36060 at yahoo.com> wrote:> > > > > > Dear R Users, > > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >