[R] Using apply function

jim holtman jholtman at gmail.com
Mon Jan 27 16:42:51 CET 2014


Is this what you want:

> random <-  expand.grid(R1 = 1:5, R2 = -(1:5))
> result <- cbind(F1 = 10, F2 = 100, random)
>
> result
   F1  F2 R1 R2
1  10 100  1 -1
2  10 100  2 -1
3  10 100  3 -1
4  10 100  4 -1
5  10 100  5 -1
6  10 100  1 -2
7  10 100  2 -2
8  10 100  3 -2
9  10 100  4 -2
10 10 100  5 -2
11 10 100  1 -3
12 10 100  2 -3
13 10 100  3 -3
14 10 100  4 -3
15 10 100  5 -3
16 10 100  1 -4
17 10 100  2 -4
18 10 100  3 -4
19 10 100  4 -4
20 10 100  5 -4
21 10 100  1 -5
22 10 100  2 -5
23 10 100  3 -5
24 10 100  4 -5
25 10 100  5 -5

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Mon, Jan 27, 2014 at 8:56 AM, Yen Lee <b88207001 at ntu.edu.tw> wrote:
> Hi all R-users,
>
> I'm trying to using apply function to input a range of values into a
> function I wrote.
> I wrote a function with 4 information needed. I would like to make 2 of them
> fixed and the other 2 random (but with specified values).
> I would like to replicate the function 10000 times.
> I was thinking about using loop function but which is really slow, therefore
> I transfer to apply function.
> But I got stucked. Could anyone help me?
>
> The question could be illustrated as follows,
> Target function: fun(F1,F2,R1,R2)
> R1 has values 1, 2, 3, 4, 5
> R2 has values -1, -2, -3, -4, -5
> F1=10
> F2=100
> There would be 25 conditions. I would like to avoid using loop to get the
> result.
> Could anyone give me some precious suggestion?
>
> Thank you!!!!
>
> Best,
> Yen
>
> ______________________________________________
> 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.




More information about the R-help mailing list