How to create a function in R with two inputs?

R ProgrammingServer Side ProgrammingProgramming

To create a function with two inputs, we just need to provide two different arguments inside function. For example, if we want to create a function to find the square of a+b then we can use x and y inside function. Check out the below examples to understand how we can do it.

Example1

Live Demo

> F<-function(x,y){x^2+y^2+2*x*y}
> F(x=1,y=1)
> F(x=2,y=3)
> F(x=c(1,2),y=c(2,3))

Output

[1] 4
[1] 25
[1] 9 25

Example

> F(x=rpois(50,2),y=rpois(50,7))

Output

[1]  36 169 121 36 49 100 144 169 144 81 100 256 121 121 36 64 49 225 121
[20] 16 64 100 36 64 36 144 49 25 196 64 324 196 144 100 81 49 196 121
[39] 16 256 81 100 64 324 81 64 144 81 121 81

Example

> F(x=rpois(120,5),y=rpois(120,10))

Output

[1]  256 169 256 225 324 81 196 169 196 121 256 196 441 361 289 144 196 441
[19] 225 144 169 625 169 100 256 144 225 144 100 196 324 169 196 144 121 225
[37] 289 400 196 400 144 256 256 196 144 169 289 225 324 400 196 196 289 121
[55] 256 256 400 169 361 225 324 144 169 144 121 169 169 361 225 169 324 289
[73] 441 196 100 144 64 196 100 121 529 289 289 196 16 400 196 289 196 441
[91] 289 324 169 225 256 225 100 225 225 289 289 81 121 256 289 324 361 361
[109] 196 225 225 324 225 81 225 256 169 144 144 361

Example2

Live Demo

> F_1<-function(x,y){x+y-2*x*y}
> F_1(x=2,y=3)
> F_1(x=75,y=83)

Output

[1] -7
[1] -12292

Example

> F_1(x=rpois(120,5),y=rpois(120,10))

Output

[1] -67 -7 -93 -97 -73 -76 -94 -38 -46 -40 -4 -159 -57 -6 -32
[16] -60 -126 -40 -112 -137 -22 -84 -49 -218 -45 -17 -32 -103 -49 -52
[31] -31 -149 -112 -149 -73 -40 -49 -31 -47 -22 -85 -19 -172 -60 -58
[46] -137 -172 -62 -60 -37 -115 -7 -84 -93 -148 -82 -123 -159 -188 -66
[61] -32 -108 -94 -52 -104 -137 -12 -9 -17 -77 -80 -112 -27 -58 -71
[76] -71 -82 -136 -67 -71 -45 -19 -175 -82 -67 -49 -82 -52 -37 -31
[91] -66 -66 -97 -62 -42 -37 -112 -172 -217 -49 -115 -172 -139 -101 -115
[106] -121 -22 -93 -62 -195 -76 -49 -112 -66 13 -31 -142 -178 -22 -121

Example

> F_1(x=sample(0:9,100,replace=TRUE),y=sample(0:9,100,replace=TRUE))

Output

[1] -22 -31 -49 8 -37 -32 -38 -49 -22 -31 -84 -8 -17 2 -31
[16] -25 -25 7 -45 -32 -27 -31 -49 -58 -22 -19 -110 -58 5 -4
[31] -25 -59 -76 -25 -67 -93 -58 -58 -4 -127 -49 1 -1 -2 9
[46] -31 -27 -59 -58 6 -93 -3 3 -67 -49 -7 2 -22 -42 -16
[61] -3 -3 -3 -84 4 -8 -49 -17 0 -32 -6 -144 -17 6 -38
[76] -144 -8 -97 2 -10 0 -10 -5 -17 -25 -110 -10 -52 -76 -84
[91] -40 -7 -10 -67 -110 -37 2 -45 -22 -17

Example3

Live Demo

> F_2<-function(x,y){log(x)*log(y)}
> F_2(x=75,y=83)
> F_2(x=7,y=4)

Output

[1] 19.07829
[1] 2.697604

Example

> F_2(x=rpois(120,5),y=rpois(120,10))

Output

[1] 3.444812 2.413898 2.882718 2.899301 4.788091 -Inf 1.348802 2.284500
[9] 2.817885 0.000000 2.975097 3.936898 4.125679 4.991161 2.729949 4.296452
[17] 2.899301 2.882718 2.413898 1.662094 3.444812 3.486603 4.986283 5.178851
[25] 4.480624 2.137801 3.175398 4.480624 0.000000 4.728556 2.634357 2.883726
[33] 3.046000 5.268714 2.882718 2.529648 1.768148 4.296452 3.999303 3.936898
[41] 4.835405 2.284500 2.634357 2.883726 1.829255 5.135368 3.754155 4.480624
[49] 3.444692 3.192061 4.835405 2.634357 4.569000 1.596030 4.986283 3.936898
[57] 4.728556 3.725859 3.131822 4.324077 3.046000 3.725859 2.483906 5.395209
[65] 4.788091 3.046000 4.595772 0.000000 2.483906 2.413898 3.486603 4.569000
[73] 2.137801 3.486603 3.210402 2.883726 4.452355 2.284500 4.125679 4.991161
[81] 5.729614 1.523000 3.936898 3.346732 6.384121 1.241953 3.999303 3.927668
[89] 4.247399 4.569000 4.728556 2.634357 0.000000 2.817885 3.444812 5.167218
[97] 5.333662 4.128127 2.231155 4.827796 2.284500 3.486603 4.569000 3.131822
[105] 3.555775 3.725859 3.046000 2.882718 4.128127 5.135368 4.125679 0.000000
[113] 3.859264 2.137801 3.859264 -Inf 5.721709 4.967812 3.444812 1.523000

Example

> F_2(x=rnorm(50,5,1),y=rnorm(50,10,2.1))

Output

[1] 4.568522 3.317414 2.464067 4.791476 4.372595 4.210160 3.526032 2.775343
[9] 3.761276 4.777044 3.505528 3.777687 4.093626 4.725572 2.424238 2.840074
[17] 3.634834 2.929286 3.752320 3.202556 4.275540 4.242939 3.874150 2.951899
[25] 2.933451 3.174667 2.880886 2.045396 4.143767 4.197051 3.701025 3.295414
[33] 2.474207 3.409486 3.039707 3.671767 3.643574 3.970818 3.783388 3.144845
[41] 2.945385 4.001539 2.084827 3.902450 3.525883 3.969045 3.836904 2.683285
[49] 3.727207 3.618169

Example

> F_2(x=runif(50,2,10),y=runif(50,1,3))

Output

[1] 2.21564775 0.96637493 1.05330228 1.32946783 2.03839496 1.31868294
[7] 1.38606698 0.60719155 0.43734519 1.39025166 1.67255979 1.18302346
[13] 1.88085789 1.33322258 1.85156672 1.48801903 0.98881685 1.56873098
[19] 0.53751024 0.21240764 1.75668136 1.23090144 1.34516355 1.27257248
[25] 1.97594694 1.12983156 1.26123428 2.19592443 0.52055000 0.78032868
[31] 1.11360152 1.08312234 1.18208915 0.48889271 0.13233468 0.49593395
[37] 1.49938567 1.71066941 1.43949609 1.63249440 0.05952467 1.10418018
[43] 0.39259085 0.21020499 0.59461948 1.78345240 2.24597820 1.99940740
[49] 0.62821888 1.40809854
raja
Published on 23-Nov-2020 09:39:47
Advertisements