[R] (newbie) sum for certain number of rows

kelvin lau kelvinlhk at yahoo.com
Wed Jul 15 12:03:51 CEST 2009


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




More information about the R-help mailing list