-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_r_functions_I.r
180 lines (139 loc) · 2.86 KB
/
simple_r_functions_I.r
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Function
dice <- 1:6
dice
mean(dice)
sample(x = dice, size = 1)
args(sample)
?sample
sample(dice, 10, replace = FALSE)
#this doesn't happen because there are less than 10 values.
sample(dice, 10, replace = TRUE)
dice <- 1:6
dice <- sample(dice,2, replace = TRUE)
sum(dice)
# my_function() <- function(){}
rolldice <- function(){
dice <- 1:6
dices <- sample(dice, 2, replace = TRUE)
sum(dices)
}
rolldice()
rolldices <- function(){
dice <- 1:6
dices <- sample(dice, 2, replace = TRUE)
print(dices)
sum(dices)
}
rolldices()
# Arguments
rolldice2 <- function(){
dices <- sample(dices, 2, replace = TRUE)
sum(dices)
}
rolldice2() # We didn't define dices yet
rolldice3 <- function(dices){
dices <- sample(dices, 2, replace = TRUE)
sum(dices)
}
rolldice3(dices = 1:6) #You can change it's value
#You can go furthermore
rolldice4 <- function(dices, x, y, z){
dices <- sample(dices, 2, replace = TRUE)
x <- sample(x, 2, replace = TRUE)
y <- sample(y, 2, replace = TRUE)
z <- sample(z, 2, replace = TRUE)
sum(dices,x,y,z)
}
rolldice4(dices = 1:6, x = 2:4, y = 4:6, z = 5:10)
f <- function(x, y = 1, z = 2){x + y + z}
f()
f.formals <- formals(f)
f.formals
pizza <- formals(f)
pizza
f.formals$y <- 3
f.formals <- formals(f)
args(f)
addTheLog <- function(first, second){
first + log(second)
}
addTheLog(second = exp(4), first = 1)
addTheLog(s = exp(4), f = 1)
addTheLog(1, exp(4))
# Example
# Fibonacci : 1, 1, 2, 3, 5, 8, 13, 21, ..., 150(if it passes 150 stop.)
myfib <- function(){
fib.a <- 1
fib.b <- 1
repeat{
temp <- fib.a + fib.b
fib.a <- fib.b
fib.b <- temp
cat(fib.a, "," , sep = "")
if (fib.b > 150){
cat("Break Now! \n")
break
}
}
}
myfib()
myfib2 <- function(thresh){
fib.a <- 1
fib.b <- 1
cat(fib.a, "," , fib.b, "," , sep = "")
repeat{
temp <- fib.a + fib.b
fib.a <- fib.b
fib.b <- temp
cat(fib.b, "," , sep = "")
if (fib.b > thresh){
cat("Break Now! \n")
break
}
}
}
myfib2(thresh = 100000000)
dummy1 <- function(){
aa <- 2.5
bb <- "string me alone"
cc <- "corona get out of here!"
dd <- 4:8
dd
}
dummy1()
dummy2 <- function(){
aa <- 2.5
bb <- "string me alone"
cc <- "corona get out of here"
dd <- 4:8
return(dd)
}
dummy2()
fib <- function(thresh){
thresh = scan()
fib.a <- 1
fib.b <- 1
cat(fib.a, ",", fib.b, ",", sep = "")
repeat{
temp <- fib.a + fib.b
fib.a <- fib.b
fib.b <- temp
cat(fib.b, "," , sep = "")
if (fib.b > thresh){
cat("Break Now! \n")
break
}
}
}
fib(thresh)
dummy3 <- function(){
aa <- 2.5
bb <- "corona, I hate you!"
return(aa)
cc <- "hello corona"
dd <- 4:8
return(dd)
}
foo1 <- dummy3()
foo1
?scan