-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.r
192 lines (155 loc) · 5.5 KB
/
tutorial.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
181
182
183
184
185
186
187
188
189
190
191
192
# comments are created with a #
# multiline comments dont exist, so just put multiple #'s
# you can type variables into the terminal and they just work,
# such as "words" for a string or 5 + 5 for a math computation
# you can print by just doing typing the words you want (ex, "hello world")
testfunction <- function(){
"Hello World"
}
# or you can use a print statement, which are good for loops
testfunction2 <- function() {
print("yep")
}
# variables are created in the moment of assignment, assigned using a <- or ->
# to print out a variables value, just type its name
num1 <- 15
16 -> num2 #does not like this one, use other
# can concatenate using the paste(x, y) function, can combine variables and text
# use the + to combine numbers
word1 <- "hello"
word2 <- "man"
words <- paste(word1, word2, "!")
nums <- num1 + num2
# you can assign multiple values in one line
var1 <- var2 <- var3 <- 13
# data types are not declared, and an item can change type after being assigned one
changevar <- 19
changevar <- "Changed"
# R data types:
# numeric- (13, 156, 174)
# integer- (1L, 55L, 483L, L declares it as an integer)
# complex- (3i, 9 + i, i is the imaginary part)
# char/string- ("k", "hello", "FALSE", "11", put it quotes)
# logical- (TRUE, FALSE)
# can use class(x) on a variable to check data type
dt1 <- 7
dt2 <- 7L
dt3 <- 6i
dt4 <- FALSE
# numeric is the most common number type, contains numbers with our without decimals
numeric1 <- 10
numeric2 <- 5.53
# integers are numbers without decimals, use when you will never have decimals
int1 <- 65
int2 <- 34
# complex hold complex numbers
comp1 <- 4i
comp2 <- 43 + 8i
comp3 <- .65i
# can convert using as.numeric(), as.integer(), as.complex()
comp1asint <- as.numeric(comp3)
num1asint <- as.integer(numeric2)
# have default math operators, +-*/, min(), max() to find min/max of a set of ints
# sqrt(), abs(), ceiling() and floor()
# ^ for exponent, %% for mod, %/% for integer division
# strings can use "" or ''
# strings can be spread along multiple lines
str1 <- "words"
str2 <- 'words,
more words,
some more words,
and finally words'
# using cat() on a string will break it into many lines in the output at natural breaks
# nchar() on a string returns the number of characters
# grepl("a", str) checks if the string "a" is present in str
# will need to add a \ before certain escape characters to make them appear in output
# when items are logically compared, it returns a logical answer
bool1 <- 0 == 0
bool2 <- 4 < 3
# can also create if statements
a <- 4
b <- 5
if(a <= b) {
print("a is less than b")
} else {
print("b is less than a")
}
#this gets printed out even if we dont run it
print("him")
#as does that
# all of these assign: <-, <<-, ->, ->>, (>> and << are global, learn later)
# : creates a series of numbers in a sequence, %in% finds out if an elements in a vector, %*% is matrix multiplication
# if, else if, else, &&, ||, nested ifs all work as expected
#while works as expected, break can be used to exit a loop while the value is true
#next can be used in a loop to skip an iteration without ending the loop
# loops are condensed, work more like an iterator than java-style loops
for(x in 1:10) {
print(x)
}
animals <- list("cat", "dog", "bird")
for(x in animals) {
print(x)
}
#can use break statement to exit loop early
#can use next to skip an iteration
#loops can be nested
# functions can be made with function keyword and can be called using the function's name and given parameters; functions can take arguments
func1 <- function() {
print("This is a function")
}
func2 <- function(name) {
paste("The names", name)
}
func3 <- function(name, age) {
paste(name, "is", age, "years old")
}
#functions can have default arguments, used if no parameter is given when expected
func4 <- function(country = "US") {
paste("I am from", country)
}
#func4() will say "I am from US", func4(Mexico) will say "I am from Mexico"
#return a value in a function with return()
func5 <- function(inta, intb) {
return(inta + intb)
}
#functions can be nested, one of two ways
#type 1: call a function with another function
nestedFunc <- function(x,y) {
a <- x + y
return(a)
}
#run with nestedFunc(nestedFunc(1,2), nestedFunc(3,4))
#type 2: write a function within a function
outFunc <- function(x) {
inFunc <- function(y) {
a <- x + y
return(a)
}
return(inFunc)
}
output <- outFunc(3)
output(5)
#You cannot directly call the function because the Inner_func has been defined (nested) inside the Outer_func.
#We need to call Outer_func first in order to call Inner_func as a second step.
#We need to create a new variable called output and give it a value, which is 3 here.
#We then print the output with the desired value of "y", which in this case is 5.
#The output will be 8
#recursion exists as expected
#variables created outside of functions are global variables, can be used by everyone inside and outside of functions
#if a local variable has the same name as a global variable, the local will be different and only be usable in the function. The global will remain as it was, global and with its original value
globtxt <- "global"
globFunc <- function() {
globtxt <- "local"
paste(globtxt)
}
#when creating variables in a function, you can make it global using the <<- operator
makeglob <- function() {
madeG <<- "made this global"
}
print(madeG)
#use the global assignment operator to change the value of a global in a variable
tempG <- "pre"
tempGFunc <- function() {
tempG <<- "post"
paste(tempG)
}