[R] Using sapply on a two argument function
Peter Ehlers
ehlers at ucalgary.ca
Fri Feb 12 00:00:15 CET 2010
How about
t(outer(seq(.1,1,.1), 1:12, foo))
Convert to dataframe, etc.
-Peter Ehlers
Steven Worthington wrote:
> Dear R users,
>
> I have a function (simplified here) that accepts two arguments and performs
> various calculations:
>
> foo <- function(y, x) {
> a <- y*sqrt(x)
> b <- a+2
> c <- a*b
> return(c)
> }
>
> If I call the function as follows I get the result I desire:
>
>> foo(.1, 1:12)
> [1] 0.2100000 0.3028427 0.3764102 0.4400000 0.4972136 0.5498979 0.5991503
> 0.6456854 0.6900000 0.7324555 0.7733250
> [12] 0.8128203
>
> or:
>
>> foo(.2, 1:12)
> [1] 0.4400000 0.6456854 0.8128203 0.9600000 1.0944272 1.2197959 1.3383005
> 1.4513708 1.5600000 1.6649111 1.7666499
> [12] 1.8656406
>
> what i'd like to do is run though a number of iterations of 'y' - say from
> .1 to 1 - for a range of x values (1:12 here, though in my actual code
> 1:100). I've had a go at automating this using sapply. The structure of the
> data.frame I create looks fine, but the content doesn't match my
> expectations. What I want is a data.frame that has the above vectors as
> columns (so, column 1 = the values for y=.1, column 2 = values for y=.2
> etc... and the rows are the values of x for any given y). Here is my attempt
> at a solution, if anyone can point out what I'm doing wrong or suggest a
> simpler method, i'd be most grateful.
>
> best,
>
> Steve
>
>
> # ------------------------------------------------
> calc <- function(w,s) {
>
> foo <- function(y=seq(.1, s/10, by=.1), x=1:w) {
> a <- y*sqrt(x)
> b <- a+2
> c <- a*b
> return(c)
> }
>
> k <- function(s=10) {
> n <- 1:s
> m <- data.frame(1:s)
> m <- sapply(n, foo)
> rownames(m) <- 1:10
> colnames(m) <- seq(.1, s/10, by=.1)
> return(m)
> }
>
> result <- k(s)
> return(result)
> }
> # ------------------------------------------------
> calc(12, 10)
>
> 0.1 0.2 0.3 0.4 0.5 0.6 0.7
> 0.8 0.9 1
> 1 3.000000 8.00000 15.00000 24.00000 35.00000 48.00000 63.0000
> 80.0000 99.0000 120.0000
> 2 4.828427 13.65685 26.48528 43.31371 64.14214 88.97056 117.7990
> 150.6274 187.4558 228.2843
> 3 6.464102 18.92820 37.39230 61.85641 92.32051 128.78461 171.2487
> 219.7128 274.1769 334.6410
> 4 8.000000 24.00000 48.00000 80.00000 120.00000 168.00000 224.0000
> 288.0000 360.0000 440.0000
> 5 9.472136 28.94427 58.41641 97.88854 147.36068 206.83282 276.3050
> 355.7771 445.2492 544.7214
> 6 10.898979 33.79796 68.69694 115.59592 174.49490 245.39388 328.2929
> 423.1918 530.0908 648.9898
> 7 12.291503 38.58301 78.87451 133.16601 201.45751 283.74902 380.0405
> 490.3320 614.6235 752.9150
> 8 13.656854 43.31371 88.97056 150.62742 228.28427 321.94113 431.5980
> 557.2548 698.9117 856.5685
> 9 15.000000 48.00000 99.00000 168.00000 255.00000 360.00000 483.0000
> 624.0000 783.0000 960.0000
> 10 16.324555 52.64911 108.97367 185.29822 281.62278 397.94733 534.2719
> 690.5964 866.9210 1063.2456
> 11 17.633250 57.26650 118.89975 202.53300 308.16625 435.79950 585.4327
> 757.0660 950.6992 1166.3325
> 12 18.928203 61.85641 128.78461 219.71281 334.64102 473.56922 636.4974
> 823.4256 1034.3538 1269.2820
>
--
Peter Ehlers
University of Calgary
More information about the R-help
mailing list