[R] How to 'apply' on multiple arguments?

Peng Yu pengyu.ut at gmail.com
Sat Sep 5 18:16:48 CEST 2009


Hi,

I tried the following code. For example, I want to add the
corresponding rows of x and y. I expect that the result be a matrix of
the same size. But the actual result is different from what I expect.
Would you please let me know what the correct command should be?

Regards,
Peng

> x=matrix(1:6,nr=2)
> y=matrix(1:6,nr=2)
> mapply(function(u,v){u + v},x,y)
[1]  2  4  6  8 10 12


On Sat, Sep 5, 2009 at 10:59 AM, Henrique Dallazuanna<wwwhsd at gmail.com> wrote:
> Yes, see mapply.
>
> On Sat, Sep 5, 2009 at 12:18 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>>
>> Hi,
>>
>> I am wonder if there is a function similar 'apply' but it could accept
>> multiple arguments?
>>
>> For example, I have the following matrix.
>> x=matrix(1:6,nr=2)
>> y=matrix(1:6,nr=2)
>>
>> I want to find a function that can be used to compute the linear
>> regression for each pair of rows in the two matrices?
>>
>> multiple_apply(x,y,1,function(u,v){lm(u ~ v)}
>>
>> That is, I wound like something like the above to compute the
>> following. Can somebody let me know if there is such an command in R?
>>
>> lm(x[1,]~y[1,])
>> lm(x[2,]~y[2,])
>>
>> Regards,
>> Peng
>>
>> ______________________________________________
>> 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.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>




More information about the R-help mailing list