-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1_basics_solutions.Rmd
175 lines (109 loc) · 4.27 KB
/
1_basics_solutions.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
---
title: "Introduction to R for Biostatistics 1"
output: html_document
---
```{r global_options, include=FALSE}
library(dplyr)
```
##In-class worksheet 1
**29 Feb to 4 Mar, 2016**
We will use R in this class mainly via R Markdown documents. R Markdown documents are documents that combine text, R code, and R output, including figures. They are a great way to produce self-contained and documented statistical analyses.
In this first worksheet, you will learn how to do some basic markdown editing and some simple data manipulation in R. After you have made a change to the document, press *Knit HTML* in R Studio and see what kind of a result you get. In addition, you should enter the code provided during the instructions part into the *Console* window below.
## Basic Markdown editing
### Instructions
Try out basic R Markdown features. You may know the Markdown language from editing wikis. Write some text that is bold, and some that is in italics. Make a numbered list and a bulleted list. Make a nested list. Write a short formula. Use the hand out for reference.
### Your assignments
This text is **bold.**
This text is *in italics.*
A numbered list:
1. Item 1
2. Item 2
3. Item 3
A bulleted list:
- Item 1
- Item 2
- Item 3
A nested list:
1. Item 1
- Item 1.1. Note that 4 spaces are required for the nesting to work properly.
- Item 1.2
2. Item 2
Formula:
$$ x = \sqrt{y}$$
## Variables, Assignments, Indexing
###Instructions
Variables in R have a name, which typically starts with a letter, is lower case and cannot contain spaces. Variables can be assigned scalar numbers. Then we can perform calculations with the variables instead of the numbers themselves. For example:
```{r}
x <- 5
y <- 7
z <- x * y
z
```
Variables can also be assigned vectors (i.e. "lists") of scalar numbers. There are special functions that allow you to quickly:
```{r, eval=FALSE}
x <- c(1,5,9,12) # create a vector with specified numbers
y <- 1:5 # create a vector containing the numbers 1 to 5
z <- rep(0,5) # create a vector with all zeros
w <- seq(0,10,by=2) # create a vector with increments of 2
```
We can use the function length to find how many elements there are in a variable:
```{r}
x <- seq(0,20,by=3)
length(x)
```
To extract the values stored at some positions of a vector variable, we can use indexing:
```{r, eval=FALSE}
x <- 1:10
x[2] # get the 2nd element
x[c(2,3)] # get the 2nd and 3rd element
x[-1] # get all but the 1st element
x[11] <- 11 # add a 11th element
```
There is also logical indexing, which returns you all entries fullfilling a certain condition.
```{r}
x <- 1:10
x[x>5] # get all elements large than 5
```
You can also store numbers in an array or matrix.
```{r}
A = matrix(seq(1,9,1), nrow=3, ncol=3)
A
```
Elements are indexed by a row and column index. Row comes first. If you omit the index, the entire row/column is returned.
```{r}
A = matrix(seq(1,9,1), nrow=3, ncol=3)
A[1,]
```
###Your assignments
If you want to try some code before putting it into the Markdown document, you can run it in the Console window below. After you hit enter, R will execute your statements. To look at a variable, just type its name and it enter. Alternatively, you can use the variable browser on the right. If you need help, just type a ? before the name function. The help window will be shown on the right. You can also use this to explore additional options, which you may need for some of the assignments.
Write code to create a vector with 5 ones and display it.
```{r}
x <- rep(1,5)
x
```
Write code to create a vector with the numbers 5, 10, 15 and so on up to 100.
```{r}
x <- seq(5,100,by=5)
x
```
Write code to create the vector `r c(1, 2, 3, 1, 2, 3, 1, 2, 3)` without having to specify all individual elements.
```{r}
x <- rep(c(1,2,3), 3)
x
```
Write code to create the vector `r c(1, 1, 1, 2, 2, 2, 3, 3, 3)` without having to specify all individual elements. Use ?rep for help.
```{r}
x <- rep(c(1,2,3), each=3)
x
```
What is the difference between the two multiplication operators for matrices?
```{r}
A <- matrix(seq(1,9,1), nrow=3, ncol=3)
A*A
A%*%A
```
Explore the use of *rowSums* and *columnSums*.
```{r}
A <- matrix(seq(1,9,1), nrow=3, ncol=3)
rowSums(A)
```