# [R] RES: applying function to multiple columns of a matrix

Federico Calboli f.calboli at imperial.ac.uk
Tue Jul 12 16:00:56 CEST 2011

```I just realised that:

apply(matrix(1:dim(my.data)[2], nrow =3), 2, function(x){my.function(my.data[,x])})

is the simplest possible method.

bw

F

On 12 Jul 2011, at 14:44, Filipe Leme Botelho wrote:

> Hi Frederico. I would keep the data as it is, create two small vectors referring to the ranges and use a mapply (as a sapply but with multiple variables) for the function. Hope the example below is helpful, although as usual someone out there will have a better solution for it.
>
>> dta <- c()
>> for (i in 1:12) dta <- cbind(dta,matrix(i,5,1))
>> dta
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [1,]    1    2    3    4    5    6    7    8    9    10    11    12
> [2,]    1    2    3    4    5    6    7    8    9    10    11    12
> [3,]    1    2    3    4    5    6    7    8    9    10    11    12
> [4,]    1    2    3    4    5    6    7    8    9    10    11    12
> [5,]    1    2    3    4    5    6    7    8    9    10    11    12
>>
>>
>> rng.a <- seq(1,10,by=3)
>> rng.b <- seq(3,12,by=3)
>> rng.a
> [1]  1  4  7 10
>> rng.b
> [1]  3  6  9 12
>> mapply(x=rng.a, y=rng.b, function(x,y) sum(dta[,c(x:y)]))
> [1]  30  75 120 165
>>
>
> Cheers,
> Filipe
>
> -----Mensagem original-----
> De: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Em nome de Federico Calboli
> Enviada em: terça-feira, 12 de julho de 2011 10:07
> Para: r-help
> Assunto: [R] applying function to multiple columns of a matrix
>
> Hi,
>
> I want to apply a function to a matrix, taking the columns 3 by 3. I could use a for loop:
>
> for(i in 1:3){ # here I assume my data matrix has 9 columns
> j = i*3
> set = my.data[,c(j-2,j-1,j)]
> my.function(set)
> }
>
> which looks cumbersome and possibly slow. I was hoping there is some function in the apply()/lapply() families that could take 3 columns at a time.
>
> I though of turning mydata in a list, then using lapply()
>
> new.data = list(my.data[,1:3], my.data[,4:6], my.data[,7:9])
> lapply(new.data, my.function)
>
> but that might incur in too much memory penalty and does have the issue of requiring a for loop to create the list (not all my data is conveniently of 9 columns only).
>
> Any suggestion would be much appreciated.
>
> Bw
>
> Federico
>
>
> --
> Federico C. F. Calboli
> Department of Epidemiology and Biostatistics
> Imperial College, St. Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel +44 (0)20 75941602   Fax +44 (0)20 75943193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>
> "This message and its attachments may contain confidential and/or privileged information. If you are not the addressee, please, advise the sender immediately by replying to the e-mail and delete this message."
>
> "Este mensaje y sus anexos pueden contener información confidencial o privilegiada. Si ha recibido este e-mail por error por favor bórrelo y envíe un mensaje al remitente."
>
> "Esta mensagem e seus anexos podem conter informação confidencial ou privilegiada. Caso não seja o destinatário, solicitamos a imediata notificação ao remetente e exclusão da mensagem."

--
Federico C. F. Calboli
Department of Epidemiology and Biostatistics
Imperial College, St. Mary's Campus
Norfolk Place, London W2 1PG

Tel +44 (0)20 75941602   Fax +44 (0)20 75943193

f.calboli [.a.t] imperial.ac.uk
f.calboli [.a.t] gmail.com

```