-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.R
145 lines (128 loc) · 2.99 KB
/
training.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
# First hour
# Installation
# User interface
# Hello world
# variables
# matrix vs data frames
# filtering
# manipulation
# functions
# loops
# aggregates
#
# Second hour
# libraries
# graphing
# time series
# further reading
# First hour
# Installation
#https://cran.r-project.org/
#https://www.rstudio.com/products/rstudio/download/
# User interface
# run a line or whole program
# Hello world
print("hello world")
# variables
firstword = "hello"
secondword <- "world"
finalword = paste(firstword,secondword,sep = "_")
my.df <- data.frame(x=1:2, y=3:4)
data()
library(datasets)
data(cars)
View(cars)
write.csv(cars, "cars.csv")
somenewdata = read.csv( "cars.csv")
# matrix vs data frames
str(somenewdata)
dim(somenewdata)
colnames(somenewdata)
matrixdata = as.matrix(somenewdata)
str(matrixdata)
somenewdata$name = "vw"
matrixdataagain = as.matrix(somenewdata)
str(matrixdataagain)
# filtering
somenewdata
somenewdata[12,]
somenewdata[12,3]
somenewdata[12,c(2,3)]
somenewdata[12,c(2,4)]
somenewdata[somenewdata$speed==12,]
somenewdata[somenewdata$speed==12 & somenewdata$dist==20 ,4]
# manipulation
colnames(somenewdata)
somenewdata = somenewdata[,-1]
somenewdata[somenewdata$speed==12,3]
somenewdata[somenewdata$speed==12,3] = "bmw"
somenewdata
somenewdata[somenewdata$speed==12 & somenewdata$dist==20 ,3] = "audi"
somenewdata
# functions
getDistance = function(brand){
originalTable = somenewdata
theRow = somenewdata[somenewdata$name == brand,]
return (theRow$dist)
}
getDistance("bmw")
getDistance("audi")
getDistance("vw")
# loops
for (i in 1:10 ){
print("hello")
}
for (i in 1:10 ){
print(i)
}
for (i in 1:10 ){
print(i*i)
}
for (i in c(1,10,12) ){
print(i*i)
}
# aggregates
min(somenewdata$speed)
max(somenewdata$speed)
mean(somenewdata$speed)
summary(somenewdata)
head(somenewdata)
tail(somenewdata)
library(dplyr)
bigsummary = somenewdata %>% group_by(name) %>% summarise(averagespeed = mean(speed), averagedist = mean(dist))
bigsummary = somenewdata %>% group_by(name,speed) %>% summarise( averagedist = mean(dist))
#
# Second hour
# libraries
library(lubridate)
date = "2019-07-08 11:58:12"
ymd(date) #fail
ymd_hms(date)
ymd_hms(date,tz="Africa/Johannesburg")
dateNoTime = "2019-07-08"
ymd_hms(dateNoTime) #fail
ymd(dateNoTime)
date(ymd_hms(date))
# graphing
plot(somenewdata$speed, somenewdata$dist)
lines(predict(lm(somenewdata$speed~somenewdata$dist)),col='green')
lines(predict(lm(somenewdata$speed~somenewdata$dist))*somenewdata$speed/3,col='green')
library(esquisse)
# time series
set.seed(123)
t <- seq(from = 1, to = 100, by = 1) + 10 + rnorm(100, sd = 7)
plot(t)
timeseries <- ts(t, start = c(2000, 1), frequency = 4)
print(timeseries)
plot(timeseries)
timeseriesdecomposed <- decompose(timeseries, type="multi")
plot(timeseriesdecomposed)
trend = timeseriesdecomposed$trend
plot(trend)
library(forecast)
forecast = holt(trend, damped=TRUE, h = 100)
plot(forecast)
forecast = holt(trend, damped=FALSE, h = 100)
plot(forecast)
#further reading
#https://www.r-bloggers.com/a-crash-course-in-r/