0

I have a large data frame 3000 columns and 3000 rows and I need to sum every 5 rows and then every 5 columns and convert my data frame into 600 rows and 600 columns: my data is like this

 matches1 matches2 matches3 matches4 matches5 ... 
    1        0        1        0        1            
    0        1        0        0        1            
    0        1        0        1        0
    1        0        1        0        1
    0        1        1        1        1

my expected results are this: my data is like this for the first 5 rows and 5columns is this:

  w1 ... w600
  14            

2 Answers2

3

We can create an index for aggregating rows and columns using gl() and then use aggregate and split.default to sum rows and columns respectively.

index <- gl(nrow(df)/5, 5)

#Sum rows
df1 <- aggregate(.~index, df, sum)[-1]

#Sum columns
sapply(split.default(df1, index), rowSums)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can create a grouping index with %/%

index <- (seq_len(nrow(df)) - 1) %/% 5 + 1

Then, use that in aggregate

aggregate(.~ index, df, sum)[-1]

Or in rowsum

rowsum(df, group = index)

It can also be used for summing the columns

sapply(split.default(df, index), rowSums)
akrun
  • 874,273
  • 37
  • 540
  • 662