-
Notifications
You must be signed in to change notification settings - Fork 1
/
course2.R
270 lines (182 loc) · 4.4 KB
/
course2.R
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
## Variables
pi
## Operators
pi * 2
1 + 2
## Assignment
foo <- "yes"
bar = "no"
foo
bar
############ Vectors in R
## character vectors
LETTERS
## integer vectors
numbers <- 1:10
numbers
## how to make custom character vectors with c()
string <- c('The', 'lazy', 'brown', 'fox')
string
############ How to use Functions
## help('grepl')
## arguments "in order"
grepl('A', LETTERS)
## arguments "by name"
grepl(x=LETTERS, pattern='A')
## Mixed arguments
grepl('A', x=LETTERS)
##########################################
## Exercise 1 (done)
##########################################
############ How to load external R packages
## using Library
library("stats4")
## listing loaded libraried
search()
## listing the contents of a loaded library
ls(2)
ls("package:stats4")
###############################
## NOW starting new material ##
###############################
############ Popular objects in R
## What class is our object?
class(LETTERS)
class(pi)
## How long is it?
length(LETTERS)
## Introducing List Objects
lst <- list(a=1, b='foo')
lst
class(lst)
length(lst)
##########################################
## Exercise 2
##########################################
## Introductin 'square' data
## matrix objects (look like vectors)
numVec <- c(1,2,3,11,12,13)
numVec
mat <- matrix(numVec, nrow = 2, ncol = 3, byrow = TRUE)
mat
## dimensions function
dim(mat)
##
length(mat)
## data.frame objects (look like lists)
df <- data.frame(number = 1:4, letter = c('A','B','C','D'))
df
dim(df)
length(df)
##
library(Biobase)
data(sample.ExpressionSet)
sample.ExpressionSet
##
## ?'ExpressionSet'
##
## ?read.table
## Loading data into R: read.table()
filename = 'refFlat.txt.gz'
foo = read.table(file=filename, nrows = 3)
foo
## Loading data: readLines()
con <- file(filename)
bar = readLines(con)
## Loading data: database connections
library(org.Hs.eg.db)
con <- org.Hs.eg_dbconn()
dbGetQuery(con, "SELECT * FROM gene_info limit 3")
##########################################
## Exercise 3
##########################################
## Data coercion: data.frame converted into a list
as.list(df)
## Data coercion: data.frame converted into a matrix
as.matrix(df)
## Data coercion: data.frame converted into a vector...
as.vector(as.matrix(df))
################# Subsetting with '['
## Numeric subsetting with a single bracket. '['
LETTERS[1]
LETTERS[c(1:3]
LETTERS[c(1,3)]
LETTERS[c(4,2)]
## Logical subsetting with a single bracket. '['
shortLetters <- LETTERS[1:4]
shortLetters[c(FALSE, TRUE, TRUE, FALSE)]
## Named subsetting with a single bracket. '['
names(shortLetters) <- c('foo','bar','baz','bob')
names(shortLetters)
shortLetters
shortLetters['foo']
shortLetters[c('bob','bar')]
## Using match()
match(shortLetters, c('C','A'))
## Using %in%
shortLetters %in% c('C','A')
c(shortLetters,shortLetters) %in% c('C','A')
## logical negation
!shortLetters %in% c('C','A')
## unique()
c(shortLetters,shortLetters)
unique(c(shortLetters,shortLetters))
##########################################
## Exercise 4
##########################################
##########################################
##########################################
## 45 min mark
##########################################
##########################################
################# Subsetting with '[['
## Numeric Subsetting with a double bracket '[['
## consider a list lst and what we get from '['
lst[1]
class(lst[1])
## Now consider '[['
lst[[1]]
class(lst[[1]])
## Now look at a data.frame with '['
df[1]
class(df[1])
## and with '[['
df[[1]]
class(df[[1]])
################# Subsetting with '[ and the second argument'
## two argument subsetting on a matrix
mat
## first row and col
mat[1,1]
## two rows and a column
mat[1:2,2]
## one row and three columns
mat[2, 1:3]
## two argument subsetting on a data.frame
df
## first row and col
df[1,1]
## three rows and a column
df[1:3,1]
## one row and two cols
df[3,1:2]
## The class won't normally change
class(df)
class(df[3,1:2])
## BUT if you slice finely enough it will change
class(df[1,1,drop=TRUE])
## For this situation you can use a THIRD argument to prevent unwanted type casting
class(df[1,1, drop=FALSE])
########### Ways to get help
##
## ?data.frame
##
## ?'data.frame'
##
## ?'GenomicRanges-class'
## The example function
example('as.numeric')
##########################################
## Exercise 5
##########################################
## 1.5 hour mark