I have a large data matrix (4460X3500) and I want to sum row subsets in groups of 10 (bin). Is there an efficient way to do this in R without using loops. Looping takes forever to perform this task! For example suppose we have the matrix> matrix(1:12,6,2)[,1] [,2] [1,] 1 7 [2,] 2 8 [3,] 3 9 [4,] 4 10 [5,] 5 11 [6,] 6 12 my problem is to sdum for example: 1. the first and second row 2. the third and fourth and 3. the fifth and the sixth. To obtain [,1] [,2] [1,] 3 15 [2,] 7 19 [3,] 11 23 Thank you. [[alternative HTML version deleted]]
What you need is matrix multiplication:
rbind( c(1,1,0,0,0,0),
c(0,0,1,1,0,0),
c(0,0,0,0,1,1) ) %*% M
Bendix
----------------------
Bendix Carstensen
Senior Statistician
Steno Diabetes Center
Niels Steensens Vej 2
DK-2820 Gentofte
Denmark
tel: +45 44 43 87 38
mob: +45 30 75 87 38
fax: +45 44 43 07 06
bxc at steno.dk
www.biostat.ku.dk/~bxc
----------------------
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
> Stephen Nyangoma
> Sent: Friday, January 14, 2005 10:18 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] summing subsets of rows matrices
>
>
> I have a large data matrix (4460X3500) and I want to sum row
> subsets in groups of 10 (bin). Is there an efficient way to
> do this in R without using loops. Looping takes forever to
> perform this task!
>
> For example suppose we have the matrix
> > matrix(1:12,6,2)
> [,1] [,2]
> [1,] 1 7
> [2,] 2 8
> [3,] 3 9
> [4,] 4 10
> [5,] 5 11
> [6,] 6 12
>
> my problem is to sdum for example:
> 1. the first and second row
> 2. the third and fourth
> and
> 3. the fifth and the sixth.
>
> To obtain
>
> [,1] [,2]
> [1,] 3 15
> [2,] 7 19
> [3,] 11 23
>
>
>
> Thank you.
> [[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
>
Hi Stephen,
you could try something like this:
mat <- matrix(rnorm(4460*3500), 4460, 3500)
###############
d <- dim(mat)
bin <- 10
system.time(out <- lapply(split(mat,rep(seq(1, d[1]/bin), each=bin)),
function(x){dim(x) <- c(bin, d[2]); colSums(x)}), gcFirst=TRUE)
out <- matrix(unlist(out, use.names=FALSE), ncol=d[2], byrow=TRUE)
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/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Stephen Nyangoma" <s.o.nyangoma at rug.nl>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, January 14, 2005 10:18 AM
Subject: [R] summing subsets of rows matrices
>I have a large data matrix (4460X3500) and I want to sum row subsets
>in groups of 10 (bin). Is there an efficient way to do this in R
>without using loops. Looping takes forever to perform this task!
>
> For example suppose we have the matrix
>> matrix(1:12,6,2)
> [,1] [,2]
> [1,] 1 7
> [2,] 2 8
> [3,] 3 9
> [4,] 4 10
> [5,] 5 11
> [6,] 6 12
>
> my problem is to sdum for example:
> 1. the first and second row
> 2. the third and fourth
> and
> 3. the fifth and the sixth.
>
> To obtain
>
> [,1] [,2]
> [1,] 3 15
> [2,] 7 19
> [3,] 11 23
>
>
>
> Thank you.
> [[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
>
Here's a simple and efficient way
A <- matrix(1:12,6,2)
nbin <- 2
dim(A) <- c(nbin, nrow(A)/nbin, 2)
B <- colSums(A)
dim(B) <- dim(A)[-1]
B
[,1] [,2]
[1,] 3 15
[2,] 7 19
[3,] 11 23
On Fri, 14 Jan 2005, Stephen Nyangoma wrote:
> I have a large data matrix (4460X3500) and I want to sum row subsets in
> groups of 10 (bin). Is there an efficient way to do this in R without
> using loops. Looping takes forever to perform this task!
>
> For example suppose we have the matrix
>> matrix(1:12,6,2)
> [,1] [,2]
> [1,] 1 7
> [2,] 2 8
> [3,] 3 9
> [4,] 4 10
> [5,] 5 11
> [6,] 6 12
>
> my problem is to sdum for example:
> 1. the first and second row
> 2. the third and fourth
> and
> 3. the fifth and the sixth.
>
> To obtain
>
> [,1] [,2]
> [1,] 3 15
> [2,] 7 19
> [3,] 11 23
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595