[R] using matrix data for function

Bing Zhang bih at ornl.gov
Wed Sep 17 20:41:25 CEST 2003


Thanks. How about I have a third parameter for the function, which is a fixed 
object? i.e. the function is f(o,x,y)

Bing

>===== Original Message From james.holtman at convergys.com =====
>Assuming that f(x,y) is not vectorize, try
>
>apply(your.matrix, 1, function(x) f(x[1], x[2]))
>
>as in:
>
>> x.1 <- matrix(1:12,ncol=2)
>> x.1
>     [,1] [,2]
>[1,]    1    7
>[2,]    2    8
>[3,]    3    9
>[4,]    4   10
>[5,]    5   11
>[6,]    6   12
>> x.f <- function(x,y) x+y
>> apply(x.1, 1, function(x) x.f(x[1], x[2]))
>[1]  8 10 12 14 16 18
>>
>__________________________________________________________
>James Holtman        "What is the problem you are trying to solve?"
>Executive Consultant  --  Office of Technology, Convergys
>james.holtman at convergys.com
>(513) 723-2929
>
>
>
>                      Bing Zhang
>                      <bih at ornl.gov>               To:       r-help 
<r-help at stat.math.ethz.ch>
>                      Sent by:                     cc:
>                      r-help-bounces at stat.m        Subject:  [R] using matrix 
data for function
>                      ath.ethz.ch
>
>
>                      09/17/2003 14:02
>
>
>
>
>
>
>Hi All,
>
>I have a function, f(x,y)
>I have a matrix of data, m,  with the 1st column is x and the 2nd column is
>y
>What's the best way to get f(x,y) for each row of the matrix?
>I tried
>result<-f(m[,1],m[,2]) but it doesn't work.
>
>Thanks!
>
>Bing
>
>---------------------------------
>1060 Commerce Park
>Oak Ridge National Laboratory
>P.O. Box 2008, MS 6480
>Oak Ridge, TN 37831-6480
>Phone: 865-241-0761
>Email: zhangb at ornl.gov
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
>
>
>--
>"NOTICE:  The information contained in this electronic mail transmission is
>intended by Convergys Corporation for the use of the named individual or
>entity to which it is directed and may contain information that is
>privileged or otherwise confidential.  If you have received this electronic
>mail transmission in error, please delete it from your system without
>copying or forwarding it, and notify the sender of the error by reply email
>or by telephone (collect), so that the sender's address records can be
>corrected."

---------------------------------
1060 Commerce Park
Oak Ridge National Laboratory
P.O. Box 2008, MS 6480
Oak Ridge, TN 37831-6480
Phone: 865-241-0761
Email: zhangb at ornl.gov




More information about the R-help mailing list