-
Notifications
You must be signed in to change notification settings - Fork 1
/
00_01_1_IntroToR_practical.qmd
145 lines (101 loc) · 2.72 KB
/
00_01_1_IntroToR_practical.qmd
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
---
title: "P01. Introduction to R, part 1"
---
**A. Create simple objects of different types in the workspace**
```{r eval = F}
item1 <- 1
item2 <- "a"
item3 <- 3.78901
```
**B. Objects with more than 1 entry are called vectors**
```{r eval = F}
# create some vectors using c() (short for concatenate)
# all items of a vector must be the same class
object1 <- c("a", "b", "c")
object2 <- 1:3
object3 <- c(1.3, -4.5, 6.99)
# you can create vectors using other named items or objects
object4 <- c(item1, item2, item3)
# take a look
object1
object2
object3
object4
```
**C. Look for your objects in the environment tab (upper right)**
**D. What class of objects is each object?**
Answer:
```{r eval = F}
# use class() to find this
class(object1)
class(object2)
class(object3)
class(object4)
```
Is object 4 the class you expected?
Answer:
**E. You can manipulate objects and make operations on them**
```{r eval = F}
object2 + 1
object3*object3
object2/object3
object1 + object2
```
what happened at each of these commands? is it what you expected?
Answer:\
**F. You can view the history of commands that were run.**
check the history of commands in the history tab (upper right)
**G. Make some other objects**
```{r eval = F}
df1 <- data.frame(ID=1:5,
animal=c("bear", "cat", "horse", "cat", "pig"),
weight=c(200, 5, 600, 8, 100))
mat1 <- matrix(data=1:50, nrow=10, ncol=5)
# view these objects
df1
mat1
View(df1)
View(mat1)
```
In R, what is the difference between a data.frame and a matrix? hint: google it.
Answer:
**H. Calculate the mean weight of the animals in df1. (i.e. mean of column 3 of df1)**
```{r eval = F}
# you can either refer to a column by name or by index
# check what the names are
colnames(df1)
# reference a column by name
df1$weight
# check how to use the function "mean"
# ? allows you to view the help file of any inbuilt function
?mean
# calculate the mean
mean(df1$weight)
mean(df1[, 3])
```
Answer:
**I. does mat1 have column names at this point?**
Answer:
```{r eval = F}
# set column names for mat1
colnames(mat1) <- c("A", "B", "C", "D", "E")
mat1
```
**J. What is the value of the element row 5, column C in mat1? what is the value of row 8, column E?**
```{r eval = F}
# inspect the element by referencing the row, then column, either by name or number
mat1[5, "C"]
mat1[5, 3]
```
Answer:
**K. Discard all of mat1 except the 1st and 4th column.**
```{r eval = F}
# subset mat1 to keep only the 1st and 4th column by number or name
mat1[ , c(1, 4)]
mat1[ , c("A", "D")]
# Create a new object of the smaller matrix.
# assign the subsetted version as a new object, mat2
mat2 <- mat1[ , c("A", "D")]
mat2 <- mat1[ , c(1, 4)]
```
The solutions to this practical can be accessed [here](00_01_1_IntroToR_solutions.qmd).