-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-Studio Notebook Functions.Rmd
109 lines (73 loc) · 1.91 KB
/
R-Studio Notebook Functions.Rmd
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: "R Notebook: Lesson 3, Functions"
output: html_notebook
---
Defining a Function
```{r}
# A function that takes zero arguments and returns nothing
foo <- function() {
print("This is my first function, foo.")
}
foo()
# A function that takes 1 argument, and returns nothing
foo <- function(x) {
print(paste("You passed ", x, "to my second function, foo."))
}
foo(88)
foo("Dweezl")
# A function that takes 2 arguments and returns one value
foo <- function(x, y=0) {
print(paste("You passed ", x, " and ", y, " to my third function, foo."))
return(x*y)
}
foo(88)
foo(11, 12)
foo(5, 7)
```
Functions and Scope
```{r}
MY_SPEED_LIMIT <<- 110
# GLOBAL x
x = 15
c = 22
b = 90
foo <- function(x) {
# LOCAL x, x that was passed to the function foo
print(paste("The value of x is ", x))
}
foo(b)
foo(c)
# Once foo exits, what is the value of x?
print(paste("The value of x is ", x))
```
A Recursive Function
A function that calls itself is called a recursive function.
A recursive function must contain a terminating condition.
Here's an example of a recursive function that terminates when the value passed to it becomes less than or equal to 1
```{r}
# Program to convert decimal number into binary number using recursive function
convert_to_binary <- function(n) {
print(paste("n is ", n))
if(n > 1) {
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}
convert_to_binary(54)
```
```{r}
convert_to_binary(101)
```
Over-riding an Infix Operator
In R, it is possible - for example - to define your own version of +, if you surround the operator with back-ticks (above the Tab key)
```{r}
# a function that adds strings together
`+` <- function(a,b) {
return(paste(a, b))
}
"Hello" + "World!"
x = "The artist is..."
y = "Claude Monet"
x + y
print("I love those haystacks by" + y)
```