apply (or similar preferred) for multiple columns

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

apply (or similar preferred) for multiple columns

Dimitris.Kapetanakis
Dear all,

I would like to use the apply or a similar function belonging to this family, but applying for each column (or row) but let say for each q columns. For example I would like to apply a function FUN for the first q columns of matrix X then for q+1:2*q and so on. If I do apply (X, 2, FUN) it applies for each column and not for every q columns. Is that possible with any similar function?

Thank you

Dimitris
Reply | Threaded
Open this post in threaded view
|

Re: apply (or similar preferred) for multiple columns

Daniel Malter
Probably not the most elegant, but a workable solution. Assume you have a matrix x of dimensions 10 x 10. Assume further you want to calculate the mean for each successive block of two columns. One way to do this is to create a matrix that indicates the column numbers from/to which to apply the function. Example:


#Random matrix
x<-rnorm(100)
dim(x)<-c(10,10)

#Create index for the first and last column in each block (your n would be 50)
n<-2

mim<-seq(from=1,to=dim(x)[2],by=n)
mox<-seq(from=2,to=dim(x)[2],by=n)

#Bind these values in matrix
mat<-cbind(mim,mox)

#Now run apply over the rows of mat, not over the data matrix
apply(mat,1,function(y){mean(x[,c(y[1]:y[2])])})



So you would replace mean(...) with whatever function you want to apply. The argument to the function must be x[,c(y[1]:y[2])], where x is your data matrix.

HTH,
Daniel


Dimitris.Kapetanakis wrote
Dear all,

I would like to use the apply or a similar function belonging to this family, but applying for each column (or row) but let say for each q columns. For example I would like to apply a function FUN for the first q columns of matrix X then for q+1:2*q and so on. If I do apply (X, 2, FUN) it applies for each column and not for every q columns. Is that possible with any similar function?

Thank you

Dimitris
Reply | Threaded
Open this post in threaded view
|

Re: apply (or similar preferred) for multiple columns

Gabor Grothendieck
In reply to this post by Dimitris.Kapetanakis
On Tue, Jul 12, 2011 at 5:27 AM, Dimitris.Kapetanakis
<[hidden email]> wrote:

> Dear all,
>
> I would like to use the apply or a similar function belonging to this
> family, but applying for each column (or row) but let say for each q
> columns. For example I would like to apply a function FUN for the first q
> columns of matrix X then for q+1:2*q and so on. If I do apply (X, 2, FUN) it
> applies for each column and not for every q columns. Is that possible with
> any similar function?
>
> Thank you
>
> Dimitris

Here is an example of using rollapply to sum all elements in the first
4 columns, then the next 4 columns, etc.   anscombe is a data set that
comes with R.  Be sure you are using zoo 1.7-0 which was just released
today.

> library(zoo)
> packageVersion("zoo")
[1] '1.7.0'
> rollapply(t(anscombe), 4, by = 4, sum, by.column = FALSE)
[1] 396.00 330.03
> c(sum(anscombe[, 1:4]), sum(anscombe[, 5:8]))
[1] 396.00 330.03


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.