-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic_R.Rmd
160 lines (137 loc) · 4.04 KB
/
basic_R.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
---
title: "Basic_R"
author: "Chengqi(Charley) Wang"
date: "1/29/2020"
output:
html_document:
df_print: paged
---
<br/>
###Arithmetic Operators
```
+ addition
- subtraction
* multiplication
/ division
^ or ** exponentiation
x %% y modulus (x mod y) 5%%2 is 1
x %/% y integer division 5%/%2 is 2
```
###Logical Operators
```
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to
!x Not x
x | y x OR y
x & y x AND y
isTRUE(x) test if X is TRUE
```
<br/><br/><br/>
###Data Types
R has a wide variety of data types including scalars, vectors,
matrices, data frames and lists.
<br/>
#####Creating new variables
```{r}
a <- 1
b <- 2
c <- a + b
```
<br/>
#####Vectors
```{r}
a <- c(1,2,5.3,6,-2,4) # numeric vector
b <- c("one","two","three") # character vector
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #logical vector
```
<br/>
####Refer to elements of a vector using subscripts
```{r}
a
a[c(2,4)] #2nd and 4th elements of vector
```
<br/>
####Matrix
All columns in a matrix must have the same mode(numeric, character, etc.) and the same length. The general format is
mymatrix <- matrix(vector, nrow=r, ncol=c, byrow=FALSE,
dimnames=list(char_vector_rownames, char_vector_colnames))
byrow=TRUE indicates that the matrix should be filled by rows. byrow=FALSE indicates that the matrix should be filled by columns (the default). dimnames provides optional labels for the columns and rows.
```{r}
# generates 5 x 4 numeric matrix
y<-matrix(1:20, nrow=5,ncol=4)
# another example
cells <- c(1,26,24,68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE,
dimnames=list(rnames, cnames))
mymatrix
```
<br/>
####Identify rows, columns or elements using subscripts.
```{r}
x <- matrix(1:16,nrow = 4,ncol = 4)
x
x[,4] # 4th column of matrix
x[3,] # 3rd row of matrix
x[2:4,1:3] # rows 2,3,4 of columns 1,2,3
```
<br/>
####Data Frames
A data frame is more general than a matrix, in that different columns can have different modes (numeric, character, factor, etc.). This is similar to SAS and SPSS datasets.
```{r}
expression <- runif(4) # random sampling four values from 0 to 1
gene <- c("gene1", "gene2", "gene3", "gene4")
f <- c(TRUE,TRUE,TRUE,FALSE)
mydata <- data.frame(Gene = gene,
Expression = expression,
pick =f)
print(mydata)
```
<br/>
####List
An ordered collection of objects (components). A list allows you to gather a variety of (possibly unrelated) objects under one name.
```{r}
# example of a list with 4 components -
# a string, a numeric vector, a matrix, and a scaler
w <- list(name = 'Marcus', days = c('2ed', '4th', '20th', '28th'),
exp = expression, height = '177cm')
w
```
Identify elements of a list using the [[]] convention.
```{r}
w[[3]] # 3rd component of the list
w[["exp"]] # component named exp in list
```
<br/>
####Factor
Tell R that a variable is nominal by making it a factor. The factor stores the nominal values as a vector of integers in the range [ 1... k ] (where k is the number of unique values in the nominal variable), and an internal vector of character strings (the original values) mapped to these integers.
```{r}
# variable gender with 20 "male" entries and
# 30 "female" entries
gender <- c(rep("male",20), rep("female", 30))
gender <- factor(gender)
# stores gender as 20 1s and 30 2s and associates
# 1=female, 2=male internally (alphabetically)
# R now treats gender as a nominal variable
summary(gender)
```
<br/>
####Useful Functions
```{r}
length(expression) # number of elements or components
str(expression) # structure of an object
class(expression) # class or type of an object
names(expression) # names
x1 <- 1:3; y1 <- 1:3
c(x1,y1) # combine objects into a vector
cbind(x1,y1) # combine objects as columns
rbind(x1,y1) # combine objects as rows
x1 # prints the object
ls() # list current objects
rm(x1) # delete an object
```