Hi R Users, I don't know how much is difficult my problem and even it is possible to solve in R or not. Given a vector with 2000 observations. I want to creat a new vector from that vector so that new vector be the sum of every 5 observations sequently. That is , each new observation is sum of every 5 sequent observations, from the first observation of previous vector to the end. So new vector must include 500 observations. My effort did not work in R. Do you have any way? Thanks a lot for any help. Amir --------------------------------- [[alternative HTML version deleted]]
Amir Safari <amsa36060 at yahoo.com> writes:> > Hi R Users, I don't know how much is difficult my problem and > even it is possible to solve in R or not. Given a vector with 2000 > observations. I want to creat a new vector from that vector so > that new vector be the sum of every 5 observations sequently. That > is , each new observation is sum of every 5 sequent observations, > from the first observation of previous vector to the end. So new > vector must include 500 observations. My effort did not work in R. > Do you have any way? Thanks a lot for any help. AmirEr, 5*500 != 2000... Did you mean "sum of 4" or "400 obs. in the new vector"? Anyways, how about colSums(matrix(myvector, nrow=5)) ? -- 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: x <- 1:20 c(rowsum(x, gl(length(x), 5, length(x))) If the length of x is not a mulitple of 5 there will be a stub at the end containing the sum of less than 5 elements. On 3/10/06, Amir Safari <amsa36060 at yahoo.com> wrote:> > > Hi R Users, > I don't know how much is difficult my problem and even it is possible to solve in R or not. > Given a vector with 2000 observations. I want to creat a new vector from that vector so that new vector be the sum of every 5 observations sequently. That is , each new observation is sum of every 5 sequent observations, from the first observation of previous vector to the end. So new vector must include 500 observations. My effort did not work in R. > Do you have any way? Thanks a lot for any help. > Amir > > > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >