forked from CrumpLab/LabJournalWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRIntrinsics.Rmd
More file actions
571 lines (414 loc) · 7.62 KB
/
RIntrinsics.Rmd
File metadata and controls
571 lines (414 loc) · 7.62 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
---
title: "R Intrinsics"
output:
html_document:
toc: true
toc_float: true
collapsed: false
number_sections: false
toc_depth: 1
#code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE, cache=TRUE)
```
This is a page that lists functions and has a brief description of how they work.
##Assignment Number 2
###General functions
1.
Help(topic)
This function shows you all the help systems for a problem
```{r}
help(sum)
```
2.
?topic
This function is used when you are looking for the help page documentation of a certain topic in R.
For example you want to understand the summing function
```{r}
?sum
```
3.
Is()
This function is used to see if a certain object in R is part of a class you selected. This is a logical statement.If it is in the class or if it is not in the class then True or False will be returned
```{r}
A<-"H"
is(A, "character")
```
4.
dir()
This function is used to create a vector of characters for a name of files or folders in a directory
```{r}
A<-"hello"
dir(A)
```
5.
list.files()
Similar to the dir function, this function creates a vector of the names of files or folders in a directory
###Input and Output
1.
save()
This function is used to create a temporary file to show that you saved your current work.
```{r}
```
2.
load()
This is used to reload what you previously saved with the save func
3.
data()
This function loads the data sets you have
```{r}
A<-c(1,2,3)
B<-c(5,6,7)
data(A,B)
```
4.
library()
This function loads, attaches, and lists packages
5.
read.csv()
This function transposes a data table to a data frame.
6.
read.table()
Similar to the previous function this reads table data and creates a data frame
7.
scan()
Reads data in a vector or list
8.
print()
This function prints out the argument
```{r}
A<-1
B<-2
print(sum(A,B))
```
9.
cat()
This concatenates outputs of objects
10.
write.table()
This prints an arguement after its converted from a data frame
###Data Creation
1.
c()
This function combines arguments into a vector.
```{r}
V<-c(1,2,3)
V
```
2.
from:to (where from and to are replaced with numbers, e.g. 1:10)
This creates a vector/variable of a data range starting from and ending to
```{r}
T<-(2:8)
T
```
3.
seq()
This will put numbers from a vector in what ever order you arrage them/set them up
```{r}
Y<-c(2:10)
seq(Y)
```
4.
rep()
This function replicates the values you choose from a vector or a list
```{r}
U<-c(2,3,4,5)
rep(U[2])
```
5.
data.frame()
This creates data in a frame that closely resembles matrices or lists
```{r}
fr<-c(1,2,3)
dr<-c(6,7,8)
data.frame(fr,dr)
```
6.
list()
This constructs and checks for differnt types of lists in R
7.
matrix()
This creates a matrix from the data given
```{r}
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)
```
8.
factor()
This labels a vector as a factor
9.
rbind()
This combines data rows
```{r}
o <- c(7, 4, 4, 9)
p <- c(5, 2, 8, 9)
k <- c(1, 2, 3, 4)
data_1 <- data.frame(o, p, k)
vec_1 <- c(9, 8, 7)
rbind(data_1, vec_1)
```
10.
cbind()
This combines data columns
###Slicing and extracting data
indexing vectors
x[n] nth element
x[-n] all but nth element
x[1:n] first n elements
x[-(1:n)] elements from n+1 to the end
x[c(1,4,2)] specific elements
x[“name”] elements named “name”
x[x>3] all elements greater than 3
x[x > 3 & x < 5] all elements between 3 and 5
x[x %in% c(“a”,“and”,“the”)] all elements in given set
###Indexing lists
x[n] list with elements n
x[[n]] nt h element of the list
x[[“name”]] element of the list named “name”
x$name id.
###Indexing matrices
x[i,j] element at row i, column j
x[i,] row i
x[,j] column j
x[,c(1,3)] columns 1 and 3
x[“name”,] row named “name”
###Indexing data frames (matrix indexing plus the following)
x[[“name”]] column named “name”
x$nameid.
###Variable conversion
1.
as.data.frame(x)
Checks is objects are a data frame
```{r}
f <- as.data.frame(matrix(1:10,2,4,9),1:5)
print(f)
```
2.
as.numeric(x)
checks is objects interpretable as numbers
```{r}
xt <- as.numeric(matrix(1:10,2,4,9),1:5)
print(xt)
```
3.
as.logical(x)
creates test of the logic in objects
4.
as.character(x)
creates test if object is a character
###Variable information
is.na(x)
is.null(x)
is.data.frame(x)
is.numeric(x)
is.character(x)
length(x)
dim(x)
dimnames(x)
nrow(x)
ncol(x)
class()
attributes()
###Data selection and manipulation
1.
which.max()
This finds the location of the maximun number in a vector
```{r}
d<-c(2,7,10,1,3)
which.max(d)
```
2.
which.min()
This finds the location of the minumum number in a vector
```{r}
w<-c(2,10,11,15,1,20)
which.min(w)
```
3.
which()
This asks which of these logical statements is true
4.
sort()
This ordered data in a vector into ascending or descending order
5.
unique()
This returns a vector like the vector created but its a duplicate
6.
table()
This builds a tables from cross classifying factors
```{r}
q<-c(2,2,3,1,4,5,5,5,6)
table(q)
```
7.
sample()
This creates a sample of data with or without replacement
```{r}
s<-c(3,5,7,9,10)
sample(s,3)
```
###Math
1.
max()
This finds the highest number in a list
```{r}
ABC<-c(1,4,17,3)
max(ABC)
```
2.
min()
This finds the smallest number in a list
```{r}
Y<-c(4,17,36,1)
min(Y)
```
3.
range()
This diplays the lowest and highest number in a list
```{r}
X<-c(3,6,9,10,13,15)
range(X)
```
4.
sum()
This adds all the the numbers you have selected
```{r}
J<-13
K<-15
sum(J,K)
```
5.
mean()
This adds all the numbers in a list and divdes it by the total number
```{r}
I<-c(1,2,3,4,5)
mean(I)
```
6.
median()
This finds the middle most number in a list
```{r}
U<-c(7,3,8,1,10,4)
median(U)
```
7.
var()
This computes the variance of a number set, i.e. how much numbers vary from mean
```{r}
l<-c(4,2,6,11,13)
var(l)
```
8.
sd()
This computes the standard deviations the numbers have from there mean
```{r}
sd(l)
```
9.
cor()
This calculates the covariance or correlation between two matrices
```{r}
D<-c(2,5,7,9,12)
cor(l,D)
```
10.
round()
This rounds a number up to o decimal places
```{r}
T<-12.6
round(T)
```
11.
abs()
This computes the absoulte value of a numer
```{r}
Q<--13
abs(Q)
```
###Matrices
1.
t()
This returns the transposed of x from a matrix or data frame
2.
diag()
This finds the diagonal of a matrix
3.
rowSums()
This finds the sum of a row in a martix or data frame
4.
colSums()
This finds the sum of a column in a matrix or data frame
5.
rowMeans()
This finds the mean of a row in a matrix
6.
colMeans()
This finds the mean of a column in a matrix
###Advanced Data processing
1.
apply()
This returns a vector/list of values that have a margin of array
2.
aggregate()
This splits data into subsets
```{r}
```
###Strings
1.
paste()
This concatenates vector
2.
strsplit()
This splits elements of a character vector
```{r}
Vec<-"AngelinaVasquez"
strsplit(Vec,split="")
```
3.
tolower()
This translates characters to lower case
```{r}
e<-"T"
tolower(e)
```
4.
toupper
This translates characters to upper case
```{r}
u<-"w"
toupper(u)
```
###Plotting
1.
hist()
This creates a histogram of given values
```{r}
w<-c(1,2,4,3,3,3,5,6,6,1)
hist(w)
```
2.
plot()
This plots r objects
###Distributions
1.
rnorm()
This runs a normal distribution
```{r}
y<-c(2,2,5,7,8,9)
rnorm(y)
```
2.
runif()
This generates random number
```{r}
runif(2:17)
```
###Programming
show that you can define a function
show that you can write a for loop
show that you can write a while loop
show that you can write an if else statement
Explain what return() does inside a function, show you can use it
Explain what break() does, show you can use it