Re: [R] (newbie) sum for certain number of rows
Thanks. I tried it and it worked wonderful. Wishing for the "DAY' to come. Life needs to be 'reset'. --- On Wed, 7/15/09, Dimitris Rizopoulos wrote: > From: Dimitris Rizopoulos > Subject: Re: [R] (newbie) sum for certain number of rows > To: "kelvin lau" > Cc: r-help@r-project.org > Date: Wednesday, July 15, 2009, 6:25 PM > one way is the following: > > dat <- read.table(textConnection( > "0 0 1 0 0 1 0 1 > 0 0 0 0 0 0 0 0 > 1 0 0 1 1 0 1 0 > 0 0 1 1 0 0 0 0 > 1 1 0 0 0 0 1 1 > 0 0 0 0 0 0 0 0 > 0 1 0 1 1 0 1 0 > 1 1 1 1 1 1 1 1 > 1 1 1 1 1 1 1 1" > )) > closeAllConnections() > > k <- 3 > ind <- rep(seq(1, nrow(dat)/k), each = k) > rowsum(dat, ind) > > > I hope it helps. > > Best, > Dimitris > > > kelvin lau wrote: > > I have following data in a data.csv file separated by > space > > > > 0 0 1 0 0 1 0 1 > > 0 0 0 0 0 0 0 0 > > 1 0 0 1 1 0 1 0 > > 0 0 1 1 0 0 0 0 > > 1 1 0 0 0 0 1 1 > > 0 0 0 0 0 0 0 0 > > 0 1 0 1 1 0 1 0 > > 1 1 1 1 1 1 1 1 > > 1 1 1 1 1 1 1 1 > > etc... > > > > I wish to calculate the sum of each column for certain > number of rows. For example if I want sum of the data after > each 3 rows, it should display > > 1 0 1 1 1 1 1 1 > > 1 1 1 1 0 0 1 1 > > 2 3 2 3 3 2 3 2 > > > > So far, this is what I have done > > xx<-read.table("data.csv",header=FALSE) > > ss<-t(apply(xx,2,sum)) # which displayed the sum of > all rows > > > > I tried my best to look for solution on the Internet > but so far haven't managed to find it. I am extremely > grateful if someone can point me how to go about it. > Thanks. > > > > Kelvin > > > > __ > > 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. > > > > -- Dimitris Rizopoulos > Assistant Professor > Department of Biostatistics > Erasmus University Medical Center > > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands > Tel: +31/(0)10/7043478 > Fax: +31/(0)10/7043014 > __ 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.
Re: [R] (newbie) sum for certain number of rows
In the zoo package, rollapply can do that. > library(zoo) > out <- rollapply(ts(dat), width = 3, by = 3, sum) > out Time Series: Start = 3 End = 9 Frequency = 0.333 V1 V2 V3 V4 V5 V6 V7 V8 3 1 0 1 1 1 1 1 1 6 1 1 1 1 0 0 1 1 9 2 3 2 3 3 2 3 2 This gives a "ts" class time series result so if you need a data frame result just convert it back: out <- as.data.frame(out) See ?rollapply for more. On Wed, Jul 15, 2009 at 6:03 AM, kelvin lau wrote: > > I have following data in a data.csv file separated by space > > 0 0 1 0 0 1 0 1 > 0 0 0 0 0 0 0 0 > 1 0 0 1 1 0 1 0 > 0 0 1 1 0 0 0 0 > 1 1 0 0 0 0 1 1 > 0 0 0 0 0 0 0 0 > 0 1 0 1 1 0 1 0 > 1 1 1 1 1 1 1 1 > 1 1 1 1 1 1 1 1 > etc... > > I wish to calculate the sum of each column for certain number of rows. For > example if I want sum of the data after each 3 rows, it should display > 1 0 1 1 1 1 1 1 > 1 1 1 1 0 0 1 1 > 2 3 2 3 3 2 3 2 > > So far, this is what I have done > xx<-read.table("data.csv",header=FALSE) > ss<-t(apply(xx,2,sum)) # which displayed the sum of all rows > > I tried my best to look for solution on the Internet but so far haven't > managed to find it. I am extremely grateful if someone can point me how to go > about it. Thanks. > > Kelvin > > __ > 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. > __ 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.
Re: [R] (newbie) sum for certain number of rows
one way is the following: dat <- read.table(textConnection( "0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1" )) closeAllConnections() k <- 3 ind <- rep(seq(1, nrow(dat)/k), each = k) rowsum(dat, ind) I hope it helps. Best, Dimitris kelvin lau wrote: I have following data in a data.csv file separated by space 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 etc... I wish to calculate the sum of each column for certain number of rows. For example if I want sum of the data after each 3 rows, it should display 1 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 2 3 2 3 3 2 3 2 So far, this is what I have done xx<-read.table("data.csv",header=FALSE) ss<-t(apply(xx,2,sum)) # which displayed the sum of all rows I tried my best to look for solution on the Internet but so far haven't managed to find it. I am extremely grateful if someone can point me how to go about it. Thanks. Kelvin __ 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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ 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.
[R] (newbie) sum for certain number of rows
I have following data in a data.csv file separated by space 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 etc... I wish to calculate the sum of each column for certain number of rows. For example if I want sum of the data after each 3 rows, it should display 1 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 2 3 2 3 3 2 3 2 So far, this is what I have done xx<-read.table("data.csv",header=FALSE) ss<-t(apply(xx,2,sum)) # which displayed the sum of all rows I tried my best to look for solution on the Internet but so far haven't managed to find it. I am extremely grateful if someone can point me how to go about it. Thanks. Kelvin __ 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.