-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession 1 - R Objects.Rmd
229 lines (150 loc) · 3.48 KB
/
Session 1 - R Objects.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
---
title: "Session 1: R Objects"
author: "Ashwin Malshe"
date: "06/01/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Atomic vectors
This next R code chunk creates and outputs a character vector. `c()` concatenates elements to form a single vector. In R even a constant is a vector of length 1. So you can use a `c()` to concatenate vectors of any length.
```{r my-chunk1}
vec1 <- c("a", "b", "c", "d", "e")
print(vec1)
```
Next chunk creates and prints a double type vector (64-bit floating type)
```{r}
vec2 <- c(3.14159, 1.414, 5.678, 9.01)
print(vec2)
```
The other two commonly used vector types in R are:
- logical
- integer
Logical vector
```{r}
vec3 <- c(rep(TRUE, 5), rep(FALSE, 6))
print(vec3)
```
Both integer and double are of type `numeric`
**Atomic vectors are homogeneous.** You can't mix different types and expect to retain those types in the same vector. For example, if you concatenate a character vector and an integer vector together, the resulting vector will be a character vector.
We can get the length of a vector by using `length()` function:
```{r}
length(vec1)
```
Vectors can have names as well:
```{r}
vec4 <- c(p = 1, q = 2, r = 3)
print(vec4)
```
Or
```{r}
vec5 <- setNames(1:3, c("p", "q", "r"))
print(vec5)
```
This helps in subsetting vectors.
```{r}
```
## Matrix
A matrix is a two-dimensional vector
```{r}
m1 <- matrix(c(3.14159, 1.414, 5.678, 9.01),
nrow = 2,
ncol = 2)
print(m1)
```
```{r}
m2 <- matrix(data = vec2, nrow = 2)
print(m2)
```
```{r}
identical(m1, m2)
```
Get the dimensions using `dim()` function
```{r}
dim(m1)
```
Subsetting a matrix is similar to a vector:
```{r}
```
## Data Frame
A tabular object that binds vectors of same lengths sideways. A data frame can contain different types of vectors. For example, the following code creates a data frame with three types of vectors - character, integer, and logical.
```{r}
d1 <- data.frame(name = c("John", "Mary", "Toby", "Maya", "Don"),
age = as.integer(c(20, 21, 45, 78, 90)),
male = c(TRUE, FALSE, TRUE, FALSE, TRUE))
print(d1)
```
Attributes of a data frame:
```{r}
attributes(d1)
```
### Subsetting a data frame
Print first column of `d1`
```{r}
print(d1$name)
```
```{r}
print(d1[, 1])
```
```{r}
print(d1[1, ])
```
Output the intersection of 2nd row and 3rd column
```{r}
print(d1[2, 3])
```
Print the column names of the data frame.
```{r}
names(d1)
```
Print the first two rows and first two columns of `d1`
```{r}
print(d1[c(1, 2), c(1, 2)])
```
```{r}
print(d1[1:2, 1:2])
```
Print the 1st and the 3rd row and the 1st and the 3rd columns
```{r}
print(d1[c(1, 3), c(1, 3)])
```
```{r}
print(d1[1:3, 1:3])
```
## Lists
Lists are a type of vector but they can be heterogeneous. They can be recursive, and have multiple lists nested within.
Commonly `json` files are read into R as lists.
```{r}
list1 <- list(vector1 = vec1,
vector2 = vec2,
matrix1 = m1,
dataframe1 = d1)
```
```{r}
print(list1)
```
```{r}
print(list1$vector1)
```
Length of a list
```{r}
length(list1)
```
Subsetting a list using `[`
```{r}
list1_1 <- list1[1]
print(list1_1)
```
```{r}
list1_2 <- list1[[1]]
print(list1_2)
```
Check the class
```{r}
class(list1_1)
```
```{r}
class(list1_2)
```
Thus, subsetting a list using `[` returns **a list containing** the requested object while using `[[` returns the actual object at that position.