-
Notifications
You must be signed in to change notification settings - Fork 45
/
Tutorial_0_R-Intro.Rmd
149 lines (121 loc) · 3.41 KB
/
Tutorial_0_R-Intro.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
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
---
title: "Tutorial 0: R Basic functions"
author: "Andreas Niekler, Gregor Wiedemann"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
html_document:
toc: true
theme: united
toc_float: true
number_sections: yes
highlight: tango
bibliography: references.bib
csl: springer.csl
---
```{r klippy, echo=FALSE, include=TRUE}
klippy::klippy()
options(width = 98)
```
Start **RStudio** on your computer. **RStudio** is a so-called IDE - Integrated Development Environment. The interface provides easy access to **R**. The advantage of this application is that **R** programs and files as well as a project directory can be managed easily. The environment is capable of editing and running program code, viewing outputs and rendering graphics. Furthermore, it is possible to view variables and data objects of an R-script directly in the interface. The screen divides into the areas
1. **R** console
2. File editor
3. Environment variables
4. Management panes (File browser, plots, help display and R packages).
```{r fig.width=10, fig.height=8, echo=FALSE, warning=FALSE}
library(png)
ima <- readPNG("resources/screenshot.png")
plot(c(100, 250), c(300, 450), type = "n", xlab = "", ylab = "", xaxt = "n",yaxt = "n")
lim <- par()
rasterImage(ima, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
```
# Basic Operations
```{r echo=TRUE, eval=F, warning=F, message=F}
1 + 2 # addition
sqrt(9) # square root function
x <- 1 # assignment
1:10 # sequence
# install.packages("quanteda")
library("quanteda")
require("quanteda")
# help
help(require)
?require
apropos("nova")
# set your working directory
setwd("~")
```
# Data types
```{r echo=TRUE, eval=F, warning=FALSE, message=FALSE}
x <- 10.5
typeof(x)
class(x)
as.integer(x)
is.integer(x)
is.integer(as.integer(x))
x <- "3.14"
typeof(x)
x <- as.double(x)
1:3 == c(1, 2, 3)
# functions
my_x_square_function <- function(x) {
result <- x ^ 2
return(result)
}
plot(my_x_square_function(seq(-5,5)), type="l")
```
# Vector, matrix and data.frames
```{r echo=TRUE, eval=F, warning=FALSE, message=FALSE}
options(stringsAsFactors = F)
myvector <- c(1, 2, 3)
names(myvector) <- c("one", "two", "three")
print(myvector)
print(myvector[1:2])
print(myvector[-1])
sum(myvector)
mean(myvector)
mymatrix <- matrix(0, nrow=3, ncol=4)
rownames(mymatrix) <- c("one", "two", "three")
colnames(mymatrix) <- c("house", "sun", "tree", ".")
mymatrix[, 1] <- 12
mymatrix[, "sun"] <- 4
mymatrix[3, 4] <- 5
mymatrix[2, 3:4] <- 9
colSums(mymatrix)
rowSums(mymatrix)
colMeans(mymatrix)
mydatdaframe <- data.frame(v = c(1, 2, 3), c = as.character(myvector), n = c("one", "two", "three"))
mydatdaframe$v
mydatdaframe$c
mydatdaframe[, "c"]
mydatdaframe[1, ]
rowSums(mydatdaframe)
```
# More basic functions
```{r echo=TRUE, eval=F, warning=FALSE, message=FALSE}
sort(mydatdaframe$n)
sort(mydatdaframe$n, decreasing = T)
c(myvector, myvector)
is.vector(myvector)
is.matrix(mymatrix)
is.matrix(myvector)
mymatrix / 2
mymatrix / myvector
t(mymatrix)
mymatrix %*% t(mymatrix)
cbind(mymatrix, myvector)
rbind(mymatrix, myvector)
# example datasets: data.frame of arrests per USA states
data(USArrests)
View(USArrests)
dim(USArrests)
nrow(USArrests)
ncol(USArrests)
length(USArrests)
max(USArrests)
which.max(USArrests[, "Murder"])
o <- order(USArrests[, "Murder"], decreasing = T)
USArrests[o, ]
murderRates <- USArrests[o[1:5], "Murder"]
names(murderRates) <- rownames(USArrests)[o[1:5]]
barplot(murderRates)
```