Data type
Vector, Numeric, Character, Logic
a <- seq(from = 1, to = 20 , by = 2) # numeric
b <- letters[1:10] # character
c <- rep(c(TRUE, FALSE), 5) # logic
a; b; c
## [1] 1 3 5 7 9 11 13 15 17 19
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## [1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
class(a); class(b); class(c)
## [1] "numeric"
## [1] "character"
## [1] "logical"
# use class() to detect the data type
Dataframe, Matrix, List
d <- matrix(1:15, ncol = 3, nrow = 5, byrow = T)
e <- as.data.frame(d) # transform matrix into dataframe
f <- list(d,e) # make it as a list
d; e; f
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
## [5,] 13 14 15
## V1 V2 V3
## 1 1 2 3
## 2 4 5 6
## 3 7 8 9
## 4 10 11 12
## 5 13 14 15
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
## [5,] 13 14 15
##
## [[2]]
## V1 V2 V3
## 1 1 2 3
## 2 4 5 6
## 3 7 8 9
## 4 10 11 12
## 5 13 14 15
class(d); class(e); class(f)
## [1] "matrix"
## [1] "data.frame"
## [1] "list"
Common orders
t(d) # transpose of a matrix
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 4 7 10 13
## [2,] 2 5 8 11 14
## [3,] 3 6 9 12 15
dim(d) # report dim of a matrix
## [1] 5 3
table(c) # compute data frequency
## c
## FALSE TRUE
## 5 5
summary(d) # summary statistic of data
## V1 V2 V3
## Min. : 1 Min. : 2 Min. : 3
## 1st Qu.: 4 1st Qu.: 5 1st Qu.: 6
## Median : 7 Median : 8 Median : 9
## Mean : 7 Mean : 8 Mean : 9
## 3rd Qu.:10 3rd Qu.:11 3rd Qu.:12
## Max. :13 Max. :14 Max. :15
head(d, n = 5L) # print out the first 5(default) row of data
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
## [5,] 13 14 15
tail(d, n = 5L) # print out the last 5(default) row of data
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
## [5,] 13 14 15
cbind(a, b, c) # bind data via columns
## a b c
## [1,] "1" "a" "TRUE"
## [2,] "3" "b" "FALSE"
## [3,] "5" "c" "TRUE"
## [4,] "7" "d" "FALSE"
## [5,] "9" "e" "TRUE"
## [6,] "11" "f" "FALSE"
## [7,] "13" "g" "TRUE"
## [8,] "15" "h" "FALSE"
## [9,] "17" "i" "TRUE"
## [10,] "19" "j" "FALSE"
rbind(a, b, c) # bind data via rows
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## a "1" "3" "5" "7" "9" "11" "13" "15" "17"
## b "a" "b" "c" "d" "e" "f" "g" "h" "i"
## c "TRUE" "FALSE" "TRUE" "FALSE" "TRUE" "FALSE" "TRUE" "FALSE" "TRUE"
## [,10]
## a "19"
## b "j"
## c "FALSE"
merge(d, e, by = "V1") # merge two data set, "by" means merge via which column
## V1 V2.x V3.x V2.y V3.y
## 1 1 2 3 2 3
## 2 4 5 6 5 6
## 3 7 8 9 8 9
## 4 10 11 12 11 12
## 5 13 14 15 14 15
Data manipulation
d[1, 2] # report the first row, second column of data
## [1] 2
d[, -2] # delete values of the second column
## [,1] [,2]
## [1,] 1 3
## [2,] 4 6
## [3,] 7 9
## [4,] 10 12
## [5,] 13 15
colnames(e); rownames(e)
## [1] "V1" "V2" "V3"
## [1] "1" "2" "3" "4" "5"
as.character(d[, 2]) # transform the specified data into character type
## [1] "2" "5" "8" "11" "14"
head(paste(d[, 1], "%", sep = ",")) # deal with character binding
## [1] "1,%" "4,%" "7,%" "10,%" "13,%"
head(round(d[, 1], 2)) # specified values rounded to 2 decimal places
## [1] 1 4 7 10 13
Simple loops
x <- 0
for( i in -10:10){
x <- (i + i^2 - i^3)
print(x)
}
## [1] 1090
## [1] 801
## [1] 568
## [1] 385
## [1] 246
## [1] 145
## [1] 76
## [1] 33
## [1] 10
## [1] 1
## [1] 0
## [1] 1
## [1] -2
## [1] -15
## [1] -44
## [1] -95
## [1] -174
## [1] -287
## [1] -440
## [1] -639
## [1] -890
x <- -1
while(x < 5) {
x = x + 1
print(x)
}
## [1] 0
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5