forked from GreatKarollo/code_r
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.r
More file actions
29 lines (21 loc) · 687 Bytes
/
functions.r
File metadata and controls
29 lines (21 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Functions
# Functions are the verbs of R.
# Functions are actions that R can do. Some actions are pre-set in R, but if they are not, we can create our own
# create a function called add.together, wherein it adds x and y (whatever those values are) together
add.together <- function(x,y) {
x+y
}
# run add.together on 2 and 5
add.together(2,5)
# To view the code that comprises a function, simply run it without any arguments
add.together
# functions can pass variables to other functions
# Notice that f() creates a variable "y", and that the y variable can be see by f()'s subfunction, g()
f <- function(x) {
y <- 1
g <- function(x) {
(x + y) / 2
}
g(x)
}
f(5)