-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_six_learning.R
More file actions
114 lines (74 loc) · 2.07 KB
/
chapter_six_learning.R
File metadata and controls
114 lines (74 loc) · 2.07 KB
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
#install packages
#install.packages('stringr')
#load package
library(stringr)
#squareRoot Function
print(sqrt(25))
#minimum function
print(min(1,6/8,4/3))
#note: you can learn the purpose of any built in function using ?FUNCTION_NAME
#other cool functions
sum_Function <- sum(1,5)
print(sum_Function)
roundFunction <- round(3.1415, 3)
print(roundFunction)
toupperFunction <- toupper('guitarplaying')
print(toupperFunction)
pasteFunction <- paste('electric, Guitar')
print(pasteFunction)
ncharFunction <- nchar('Can you tell I play guitar')
print(ncharFunction)
cFunction <- c(1,3,5,7,9)
print(cFunction)
seqFunction <- seq(1,200)
print(seqFunction)
#named arguments
#optional arguments in a function in which you can specify that an argument value has
#a particular name. As a result, you do not need to remember the order of operational
#arguments, you just need to reference them by name
argumentsByNameEx <- paste('Gibson', 'Les Paul', sep = "-----")
print(argumentsByNameEx)
#because named arguments are optional, they can be included in any order
#the following all do the same thing
round(3.1415, 3)
round(3.1415, digits = 3)
round(digits = 3, 3.1415)
#also: mathematical operators are also functions
x <- '+'(5, 3)
print(x)
x <- '-'(5, 3)
print(x)
x <- '/'(5, 3)
print(x)
x <- '*'(5, 3)
print(x)
#using stringr package
#count characters in a string
strCountFunction <- str_count("Mississippi", "i")
print(strCountFunction)
#making a function
firstFunction <- function(radius)
{
area = pi * radius * radius
#functions return the value of the last line
area
#note: you can use a 'return()' function but it is best practice to only use that to return a value before the last statement is executed
}
circleAreaUsingCustomFunction <- firstFunction(2)
print(circleAreaUsingCustomFunction)
#remember, order matters in R programming
#conditional statements
if( circleAreaUsingCustomFunction > 12)
{
print('yay')
}
if( circleAreaUsingCustomFunction < 6)
{
print('yay')
} else if ( circleAreaUsingCustomFunction < 10)
{
print('boo')
} else
{
print('meh')
}