Re: [R] Function modification: how to calculate values for every combination?
Just to add to this be sure you do have names if you want them and read about vectorization in ?outer in case fun was just an example and your actual fun is more complex: x <- c(1,2,3) names(x) <- x y <- c(4,5,6) names(y) <- y outer(x, y, fun) # as in previous answer # or outer(-log(15) * x, log(10) * y, "+") On 9/2/07, Erich Neuwirth <[EMAIL PROTECTED]> wrote: > outer(x,y,fun) > > Lauri Nikkinen wrote: > > Hello, > > > > I have a function like this: > > > > fun <- function (x, y) { > > a <- log(10)*y > > b <- log(15)*x > > extr <- a-b > > extr > > } > > > > fun(2,3) > > [1] 1.491655 > > > > x <- c(1,2,3) > > y <- c(4,5,6) > > fun(x, y) > > [1] 6.502290 6.096825 5.691360 > > > > How do I have to modify my function that I can calculate results using > > every combination of x and y? I would like to produce a matrix which > > includes the calculated values in every cell and names(x) and names(y) > > as row and column headers respectively. Is the outer-function a way to > > solution? > > > > Best regards, __ R-help@stat.math.ethz.ch 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.
Re: [R] Function modification: how to calculate values for every combination?
outer(x,y,fun) Lauri Nikkinen wrote: > Hello, > > I have a function like this: > > fun <- function (x, y) { > a <- log(10)*y > b <- log(15)*x > extr <- a-b > extr > } > > fun(2,3) > [1] 1.491655 > > x <- c(1,2,3) > y <- c(4,5,6) > fun(x, y) > [1] 6.502290 6.096825 5.691360 > > How do I have to modify my function that I can calculate results using > every combination of x and y? I would like to produce a matrix which > includes the calculated values in every cell and names(x) and names(y) > as row and column headers respectively. Is the outer-function a way to > solution? > > Best regards, > Lauri > > __ > R-help@stat.math.ethz.ch 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. > > -- Erich Neuwirth, University of Vienna Faculty of Computer Science Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-39464 Fax: +43-1-4277-39459 __ R-help@stat.math.ethz.ch 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.
Re: [R] Function modification: how to calculate values for every combination?
On 9/2/07, Paul Smith <[EMAIL PROTECTED]> wrote: > > I have a function like this: > > > > fun <- function (x, y) { > > a <- log(10)*y > > b <- log(15)*x > > extr <- a-b > > extr > > } > > > > fun(2,3) > > [1] 1.491655 > > > > x <- c(1,2,3) > > y <- c(4,5,6) > > fun(x, y) > > [1] 6.502290 6.096825 5.691360 > > > > How do I have to modify my function that I can calculate results using > > every combination of x and y? I would like to produce a matrix which > > includes the calculated values in every cell and names(x) and names(y) > > as row and column headers respectively. Is the outer-function a way to > > solution? > > Try the following code and adapt it to fill the matrix: > > fun <- function (x, y) { > a <- log(10)*y > b <- log(15)*x > extr <- a-b > extr > } > > x <- c(1,2,3) > y <- c(4,5,6) > > combs <- expand.grid(x,y) > > for (i in 1:nrow(combs)) > cat(fun(combs[i,1],combs[i,2]),"\n") The complete code can be: fun <- function (x, y) { a <- log(10)*y b <- log(15)*x extr <- a-b extr } x <- c(1,2,3) y <- c(4,5,6) combs <- expand.grid(x,y) a <- vector() for (i in 1:nrow(combs)) a[i] <- fun(combs[i,1],combs[i,2]) m <- matrix(a,3,3) rownames(m) <- x colnames(m) <- y m Paul __ R-help@stat.math.ethz.ch 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.
Re: [R] Function modification: how to calculate values for every combination?
Yeah, exactly. Thanks. The solution was too obvious :-) Cheers, Lauri 2007/9/2, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > > Hello, > > > > I have a function like this: > > > > fun <- function (x, y) { > > a <- log(10)*y > > b <- log(15)*x > > extr <- a-b > > extr > > } > > > > fun(2,3) > > [1] 1.491655 > > > > x <- c(1,2,3) > > y <- c(4,5,6) > > fun(x, y) > > [1] 6.502290 6.096825 5.691360 > > > > How do I have to modify my function that I can calculate results using > > every combination of x and y? I would like to produce a matrix which > > includes the calculated values in every cell and names(x) and names(y) > > as row and column headers respectively. Is the outer-function a way to > > solution? > > > > Best regards, > > Lauri > > > > __ > > R-help@stat.math.ethz.ch 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. > > > > > > How about > > outer(x,y,fun) > > ? > > hth > > D. Trenkler > > __ R-help@stat.math.ethz.ch 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.
Re: [R] Function modification: how to calculate values for every combination?
On 9/2/07, Lauri Nikkinen <[EMAIL PROTECTED]> wrote: > I have a function like this: > > fun <- function (x, y) { > a <- log(10)*y > b <- log(15)*x > extr <- a-b > extr > } > > fun(2,3) > [1] 1.491655 > > x <- c(1,2,3) > y <- c(4,5,6) > fun(x, y) > [1] 6.502290 6.096825 5.691360 > > How do I have to modify my function that I can calculate results using > every combination of x and y? I would like to produce a matrix which > includes the calculated values in every cell and names(x) and names(y) > as row and column headers respectively. Is the outer-function a way to > solution? Try the following code and adapt it to fill the matrix: fun <- function (x, y) { a <- log(10)*y b <- log(15)*x extr <- a-b extr } x <- c(1,2,3) y <- c(4,5,6) combs <- expand.grid(x,y) for (i in 1:nrow(combs)) cat(fun(combs[i,1],combs[i,2]),"\n") Paul __ R-help@stat.math.ethz.ch 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.
[R] Function modification: how to calculate values for every combination?
Hello, I have a function like this: fun <- function (x, y) { a <- log(10)*y b <- log(15)*x extr <- a-b extr } fun(2,3) [1] 1.491655 x <- c(1,2,3) y <- c(4,5,6) fun(x, y) [1] 6.502290 6.096825 5.691360 How do I have to modify my function that I can calculate results using every combination of x and y? I would like to produce a matrix which includes the calculated values in every cell and names(x) and names(y) as row and column headers respectively. Is the outer-function a way to solution? Best regards, Lauri __ R-help@stat.math.ethz.ch 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.