-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_R_basics.Rmd
executable file
·241 lines (195 loc) · 6.34 KB
/
1_R_basics.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
title: "1. Small introduction to R and RStudio"
author: "[G. Moroz](mailto:agricolamz@gmail.com) <br> Presentation link: goo.gl/HdS466"
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE, comment=NA)
```
<style>
.parallax {
/* The image used */
background-image: url("logo.png");
/* Set a specific height */
min-height: 300px;
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: auto;
}
</style>
<div class="parallax"></div>
## 0. Prerequisites:
* install [R](https://cran.r-project.org/)
* install [RStudio Desktop](http://www.rstudio.com/download) (it is not necessary, but RStudio makes a lot of things easier)
<div class="parallax"></div>
## 1. R
To download R, go to [CRAN](https://cran.r-project.org/). Don’t try to pick a mirror that’s close to you, instead it is better to use the cloud mirror, https://cloud.r-project.org.
<div class="parallax"></div>
## 2. RStudio
RStudio is an integrated development environment, or IDE, for R programming. There are two possibilities:
* you type R code in the R console pane, and press enter to run it
* you type R code in the Code editor pane, and press control/command + enter to run selected part

<div class="parallax"></div>
## 3. Some R basics
### 3.1 Base elements
```{r}
7
-5.7
"bonjour"
"bon mot"
TRUE
FALSE
```
### 3.2 Variables
```{r}
my_var <- 7
my_var
my_var+7
my_var
my_var <- my_var + 7
```
### 3.2 Vectors
```{r}
5:9
11:4
numbers <- c(7, 9.9, 24)
multiple_strings <- c("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog")
one_string <- c("the quick brown fox jumps over the lazy dog")
true_false <- c(TRUE, FALSE, FALSE, TRUE)
length(numbers)
length(multiple_strings)
length(one_string)
```
### 3.3 Dataframe
```{r}
my_df <- data.frame(latin = c("a", "b", "c"),
cyrillic = c("а", "б", "в"),
greek = c("α", "β", "γ"),
numbers = c(1:3),
is.vowel = c(TRUE, FALSE, FALSE),
stringsAsFactors = FALSE)
my_df
nrow(my_df)
ncol(my_df)
```
### 3.4 Indexing
```{r}
numbers[3]
multiple_strings[9]
my_df[2, 3]
my_df[2,]
my_df[,3]
my_df$is.vowel
my_df$is.vowel[2]
```
### 3.5 Reading files to R
We can read to R a dataset about Numeral Classifiers from [AUTOTYP database](https://github.com/autotyp/autotyp-data).
```{r}
new_df <- read.csv("https://raw.githubusercontent.com/autotyp/autotyp-data/master/data/Numeral_classifiers.csv")
new_df
```
It could be also a file on your computer, just provide a whole path to the file. Windows users need to change backslashes `\` to slashes `/`.
```{r, eval = FALSE}
new_df_2 <- read.csv("/home/agricolamz/my_file.csv")
```
### 3.6 Writing files from R
```{r, eval = FALSE}
write.csv(new_df_2, "/home/agricolamz/my_new_file.csv",
row.names = FALSE)
```
### 3.7 Missing data
In R, missing values are represented by the symbol `NA` (not available).
```{r}
is.na(new_df$NumClass.Presence)
sum(is.na(new_df$NumClass.Presence))
sum(is.na(new_df))
```
### 3.8 How to get help in R
```{r, eval = FALSE}
?nchar
```
## 3.9 Installing packages
There are a lot of R packages for solving a lot of different problems. There are two way for install them (you need an internet connection):
* packages on CRAN are checked in multiple ways and should be stable
```{r, eval=FALSE}
install.packages("lingtypology")
```
* packages on GitHub are NOT checked and could contain anything, but it is the place where all package developers keep the last vertion of they work.
```{r, eval=FALSE}
install.packages("devtools")
devtools::install_github("ropensci/lingtypology")
```
* or package file
```{r, eval=FALSE}
install.packages("lingtypology",
destdir = "/path/to/your/package")
```
After the package is installed you need to load the package using the following command:
```{r, eval=FALSE}
library("lingtypology")
```
There is a nice picture from [Phillips N. D. (2017) YaRrr! The Pirate’s Guide to R](https://bookdown.org/ndphillips/YaRrr/):

<div class="parallax"></div>
## 4. Tasks
### 4.1
Install `lingtypology` package and find, how many rows in the `circassian` dataset.
<form name="FormOne" onsubmit="return validateFormOne()" method="post">
<input type="text" name="answerOne">
<input type="submit" value="check">
</form>
### 4.2
In the `circassian` dataset find out the name of the 86. village.
<form name="FormTwo" onsubmit="return validateFormTwo()" method="post">
<input type="text" name="answerTwo">
<input type="submit" value="check">
</form>
### 4.3
Write the `circassian` dataset to your computer and use `file.info()` function to figure out how many bites in the result file.
<form name="FormThree" onsubmit="return validateFormThree()" method="post">
<input type="text" name="answerThree">
<input type="submit" value="check">
</form>
<div class="parallax"></div>
## 5. Further reading
There are a lot of books, articles, tutorials and online courses about R. Here is some of them:
* [book _"R for Data Science"_ by G. Grolemund and H. Wickham](http://r4ds.had.co.nz/)
* [on-line course _"Introduction à R"_ by V. Guyader](https://www.datacamp.com/community/open-courses/introduction-a-r)
* [book _"How to do Linguistics with R"_ by N. Levshina](https://benjamins.com/#catalog/books/z.195/main)
* [book _"Statistics for Linguistics with R"_ by S. Gries](https://www.amazon.com/dp/3110307286)
<script>
function validateFormOne() {
var x = document.forms["FormOne"]["answerOne"].value;
if (x != "157") {
alert("I have a different answer...");
return false;
} else {
alert("Exactly!");
return false;
}
}
function validateFormTwo() {
var y = document.forms["FormTwo"]["answerTwo"].value;
if (y != "Vochepshiy") {
alert("I have a different answer...");
return false;
} else {
alert("Exactly!");
return false;
}
}
function validateFormThree() {
var z = document.forms["FormThree"]["answerThree"].value;
if (z != "9315") {
alert("I have a different answer...");
return false;
} else {
alert("Exactly!");
return false;
}
}
</script>