[R] re sults from "do.call" function
Charles C. Berry
cberry at tajo.ucsd.edu
Mon Nov 17 03:37:07 CET 2008
On Sun, 16 Nov 2008, kathie wrote:
func needs to return a result of the same length as the input vectors
try this:
func2 <- function(i,y0,j) { y0* exp( rowSums(X[i,] * t(B[,j])) ) }
do.call(func2, list1 )
Chuck
>
> Dear R users...
>
> I made this by help of one of R users.
> _________________________________________________________________
>
> X=matrix(seq(1,4), 2 , 2)
> B=matrix(c(0.6,1.0,2.5,1.5) , 2 , 2)
>
> func <- function(i,y0,j) { y0*exp(X[i,]%*%B[,j]) }
> list1 <- expand.grid( i=c(1,2) , y0=c(1,2) , j=c(1,2) )
> results <- do.call( func , list1 )
> _________________________________________________________________
>
>> results
> [,1] [,2] [,3] [,4] [,5]
> [,6] [,7] [,8]
> [1,] 36.59823 36.59823 36.59823 36.59823 1096.633 1096.633
> 1096.633 1096.633
> [2,] 181.27224 181.27224 181.27224 181.27224 59874.142 59874.142
> 59874.142 59874.142
> [3,] 73.19647 73.19647 73.19647 73.19647 2193.266 2193.266
> 2193.266 2193.266
> [4,] 362.54448 362.54448 362.54448 362.54448 119748.283 119748.283
> 119748.283 119748.283
> [5,] 36.59823 36.59823 36.59823 36.59823 1096.633 1096.633
> 1096.633 1096.633
> [6,] 181.27224 181.27224 181.27224 181.27224 59874.142 59874.142
> 59874.142 59874.142
> [7,] 73.19647 73.19647 73.19647 73.19647 2193.266 2193.266
> 2193.266 2193.266
> [8,] 362.54448 362.54448 362.54448 362.54448 119748.283 119748.283
> 119748.283 119748.283
> _________________________________________________________________
>
>
> However, I expected only this below as you imagine.
> [,1]
> [1,] 36.59823
> [2,] 181.27224
> [3,] 73.19647
> [4,] 362.54448
>
> [,5]
> [1,] 1096.633
> [2,] 59874.142
> [3,] 2193.266
> [4,] 119748.283
>
> That is, from "list1",
>
> y0*exp(X[i,]%*%B[,j])
> ------------------------------------
> 1*exp(X[1,]%*%B[,1]) = 36.59823
> 1*exp(X[2,]%*%B[,1]) = 181.27224
> 2*exp(X[1,]%*%B[,1]) = 73.19647
> 2*exp(X[2,]%*%B[,1]) = 362.54448
>
> 1*exp(X[1,]%*%B[,2]) = 1096.633
> 1*exp(X[2,]%*%B[,2]) = 59874.142
> 2*exp(X[1,]%*%B[,2]) = 2193.266
> 2*exp(X[2,]%*%B[,2]) = 119748.283
> ------------------------------------
>
> How could I find only 8 values? Any suggestion will be greatly appreciated.
>
> Kathryn Lord
> --
> View this message in context: http://www.nabble.com/results-from-%22do.call%22-function-tp20532136p20532136.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list