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.
Dear Kathie, Try this: X=matrix(seq(1,4), 2 , 2) B=matrix(c(0.6,1.0,2.5,1.5) , 2 , 2) list1 <- expand.grid( i=c(1,2) , y0=c(1,2) , j=c(1,2) ) apply(list1,1,function(x) { i=x[1] y0=x[2] j=x[3] y0*exp(X[i,]%*%B[,j]) } ) # [1] 36.59823 181.27224 73.19647 362.54448 1096.63316 59874.14172 2193.26632 # [8] 119748.28343 HTH, Jorge On Sun, Nov 16, 2008 at 7:49 PM, kathie <kathryn.lord2000@gmail.com> wrote:> > 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@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. >[[alternative HTML version deleted]]
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