[R] more efficient sum of matrix columns

Jean Eid jeaneid at chass.utoronto.ca
Sun Jun 15 16:15:48 CEST 2003


this is what my program read
dataa<-data.frame(matrix(0, nrow=nrow(data), ncol=ncol(data))
i<-1
j<-1
while(i<=nrow(data))
{
   if(data$Index.Price==1)
	data1[j,]<-data[i,]
   else
{
	num<-data$No.Primary[i]
	data[j,]<-apply(data[i:i+num-1,],2,sum)
	i<-i+num-1
}
   j<-j+1
   i<-i+1
}
#data is the original 4462*202 matrix.

thanks,



On Sat, 14 Jun 2003, John Fox wrote:

> Dear Jean and Jonathan,
>
> colSums() should be more efficient, but (unless I misunderstand the size of
> the problem) a problem this small shouldn't take a half hour. On my ageing
> 800 MHz, 512MB Windows 2000 PC, the result was essentially instantaneous
> either way, though an order of magnitude faster with colSums:
>
>  > data <- matrix(rnorm(4462 * 202), 4462, 202)
>  > dim(data)
> [1] 4462  202
>  > system.time(apply(data, 2, sum))
> [1] 0.38 0.00 0.42   NA   NA
>  > system.time(colSums(data))
> [1] 0.03 0.00 0.03   NA   NA
>
> Jean does make reference to summing parts of a matrix, apparently in a
> loop, so it might help to know what's being done besides the column sums.
>
> Regards,
>   John
>
> At 09:18 PM 6/14/2003 -0400, Jonathan Baron wrote:
> >On 06/14/03 20:51, Jean Eid wrote:
> > >Dear R users,
> > >I am looking for a more efficient way to compute the sum of columns of a
> > >matrix.
> > >I am currently using apply(data, 2, sum) however, I am building a data set
> > >from another one by summing the columns of some parts of the matrix.
> > >the loop is taking too long (about 1/2 hour) for a 4462 * 202 matrix.
> >
> >colSums() might be faster, but I don't know how much faster.  It
> >does not allow na.rm=T, but you don't have that, so it might
> >help.
>
> -----------------------------------------------------
> John Fox
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada L8S 4M4
> email: jfox at mcmaster.ca
> phone: 905-525-9140x23604
> web: www.socsci.mcmaster.ca/jfox
> -----------------------------------------------------
>
>




More information about the R-help mailing list