[R] functions on rows or columns of two (or more) arrays

2011-08-04 Thread Jim Bouldin
I realize this should be simple, but even after reading over the several help pages several times, I still cannot decide between the myriad apply functions to address it. I simply want to apply a function to all the rows (or columns) of the same index from two (or more) identically sized arrays

Re: [R] functions on rows or columns of two (or more) arrays

2011-08-04 Thread R. Michael Weylandt
I hope someone experience with plyr package comes and helps because this sounds like what it does well, but for your specific example something like this works: A = rbind(a,a2) q = apply(A,2,function(x) {lm(x[1:nrow(a)] ~ x[-(1:nrow(a))])}) but yeah, that's pretty rough so I hope someone can

Re: [R] functions on rows or columns of two (or more) arrays

2011-08-04 Thread Florent D.
The apply function also works with multi-dimensional arrays, I think this is what you want to achieve using a 3d array: aaa - array(NA, dim = c(2, dim(a))) aaa[1,,] - a aaa[2,,] - a2 apply(aaa, 3, function(x)lm(x[1,]~x[2,])) __ R-help@r-project.org

Re: [R] functions on rows or columns of two (or more) arrays

2011-08-04 Thread Dennis Murphy
Hi: Here's one approach: a=matrix(1:50,nrow=10) a2=floor(jitter(a,amount=50)) # Write a function to combine the columns of interest # into a data frame and fit a linear model regfn - function(k) { rdf - data.frame(x = a[k, ], y = a2[k, ]) lm(y ~ x, data = rdf) } # Use lapply() to