[R] function on columns of two arrays

Folkes, Michael Michael.Folkes at dfo-mpo.gc.ca
Tue Aug 20 19:03:19 CEST 2013


I know have multiple ways to skin the metaphorical cat.
Thanks to Jason and A.K.
Michael 

-----Original Message-----
From: Law, Jason [mailto:Jason.Law at portlandoregon.gov] 
Sent: August 20, 2013 9:54 AM
To: Folkes, Michael
Cc: R help
Subject: RE: function on columns of two arrays

library(abind)
library(plyr)
c <- abind(a,b, along = 4)
results <- alply(c, c(2,3), function(x) lm(x[,2] ~ x[,1]))
ldply(results, function(x) summary(x)$coef)

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Folkes, Michael
Sent: Tuesday, August 20, 2013 12:34 AM
To: r-help at r-project.org
Subject: [R] function on columns of two arrays

I've spent a bit too long searching the help history and attempting to
apply some logic to the following:
I have two 3D arrays each with same dim. I wish to run lm on the
respective columns of each array, preferably without loops.
We often hear chatter that sometimes apply() won't be faster "just use a
for loop" I'd like to test this one...
I just can't seem to wrap my brain around use of mapply on this task and
am more surprised that I'm not finding a solution out there already.


a <- array(1:60,dim=5:3)
b <- a*3+10
lm(b[,1,1]~a[,1,1])
#and repeat for all rows and columns...

thanks in advance.
Michael

_______________________________________________________
Michael Folkes
Salmon Stock Assessment
Canadian Dept. of Fisheries & Oceans     
Pacific Biological Station

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list