March, 2018
x1 = 3.5 typeof(x1)
## [1] "double"
x2 = 3 typeof(x2)
## [1] "double"
x3 = 3L typeof(x3)
## [1] "integer"
x4 = "a" typeof(x4)
## [1] "character"
x5 = TRUE typeof(x5)
## [1] "logical"
x6 = x1 + x3 typeof(x6)
## [1] "double"
c()
creates 1-dimensional array called of vector in mathematics.x = c(3) print(x)
## [1] 3
x = c(88, 15, 12, 13) x
## [1] 88 15 12 13
:
is frequentyly used.1:5
## [1] 1 2 3 4 5
3:(-2)
## [1] 3 2 1 0 -1 -2
(3.1):(5.5)
## [1] 3.1 4.1 5.1
c()
can take multiple vectors. For example,x1 = 1:3 x2 = 10:5 x3 = c(x2,x1) x3
## [1] 10 9 8 7 6 5 1 2 3
seq()
y = seq(from = 12, to = 30, by = 2) y
## [1] 12 14 16 18 20 22 24 26 28 30
y = seq(12, 30, length = 19) y
## [1] 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
rep()
x = rep(8, 4) x
## [1] 8 8 8 8
x = rep( c(5, 12, 13), 3) x
## [1] 5 12 13 5 12 13 5 12 13
x = rep( c(5, 12, 13), each = 3) x
## [1] 5 5 5 12 12 12 13 13 13
[]
is used to specifying indices.x = 5:8 x1 = x[1:3] x1
## [1] 5 6 7
x2 = c(x1[1:3], 10, x[4]) x2
## [1] 5 6 7 10 8
y = c(1.2, 3.9, 0.4, 0.12) y[c(1, 3)]
## [1] 1.2 0.4
y[2:3]
## [1] 3.9 0.4
v = 3:4 y[v]
## [1] 0.40 0.12
y[c(1, 1, 2)]
## [1] 1.2 1.2 3.9
y[-c(1,2)]
## [1] 0.40 0.12
y[-length(y)]
## [1] 1.2 3.9 0.4
length()
is used to return the length of a vector.x = 3:6 length(x)
## [1] 4
1:length(x)
## [1] 1 2 3 4
x1 = c(5,0, -4) x2 = c(1, 2, 2)
x1+x2
## [1] 6 2 -2
x1*x2
## [1] 5 0 -8
x1/x2
## [1] 5 0 -2
x1 + 3
## [1] 8 3 -1
x1*2
## [1] 10 0 -8
x1 = c(5,0,-4,2) x2 = c(2,1) x1/x2
## [1] 2.5 0.0 -2.0 2.0
y = 1:10-1 y
## [1] 0 1 2 3 4 5 6 7 8 9
y = 1:(10-1) y
## [1] 1 2 3 4 5 6 7 8 9
x <- 11 x^2
## [1] 121
x%/%5
## [1] 2
x%%5
## [1] 1
6<=7
## [1] TRUE
6==7
## [1] FALSE
6!=7
## [1] TRUE
z = c(5, 2, -3, 8) z^2
## [1] 25 4 9 64
z^2 > 8
## [1] TRUE FALSE TRUE TRUE
z[z>0]
## [1] 5 2 8
z[z^2>8]
## [1] 5 -3 8
x = c(3,1,4,1) x[x>5]
## numeric(0)
which(x>5)
## integer(0)
x[which(x>5)]
## numeric(0)
%in%
function1 %in% c(2,1,4)
## [1] TRUE
c(1,5) %in% c(2,1,5)
## [1] TRUE TRUE
c(1,5,3) %in% c(2,1,5)
## [1] TRUE TRUE FALSE
x = c(3,1,4,1) x%in% c(2,1,4)
## [1] FALSE TRUE TRUE TRUE
match()
functionmatch(1 ,c(2,1,4))
## [1] 2
match(c(1,4),c(2,1,4))
## [1] 2 3
x <- c(3,1,4,1) match(x ,c(2,1,4))
## [1] NA 2 3 2
y = matrix( c(1, 2, 3, 4), nrow = 2, ncol = 2) y
## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4
y = matrix( c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = T) y
## [,1] [,2] ## [1,] 1 2 ## [2,] 3 4
y = matrix(c(1,3,4,5,1,3,4,1),4,2) y[1, 1]
## [1] 1
y[, 1]
## [1] 1 3 4 5
y[-2,]
## [,1] [,2] ## [1,] 1 1 ## [2,] 4 4 ## [3,] 5 1
class(y)
## [1] "matrix"
dim(y)
## [1] 4 2
ncol(y)
## [1] 2
nrow(y)
## [1] 4
one = rep(1,4) z = matrix( c(5:8, rep(-1,4), rep(0,4)), 4, 3) cbind(one, z)
## one ## [1,] 1 5 -1 0 ## [2,] 1 6 -1 0 ## [3,] 1 7 -1 0 ## [4,] 1 8 -1 0
z = rbind(2, z) z
## [,1] [,2] [,3] ## [1,] 2 2 2 ## [2,] 5 -1 0 ## [3,] 6 -1 0 ## [4,] 7 -1 0 ## [5,] 8 -1 0
x = 1:10 x[1] = "a" typeof(x[2])
## [1] "character"
x = matrix(1:4,2,2) x[2,2] = "b" typeof(x)
## [1] "character"
kids = c("Jack", "Jill") ages = c(12, 10) d = data.frame(kids, ages, stringsAsFactors = F) d
## kids ages ## 1 Jack 12 ## 2 Jill 10
str(d)
## 'data.frame': 2 obs. of 2 variables: ## $ kids: chr "Jack" "Jill" ## $ ages: num 12 10
d$ages
## [1] 12 10
class(d$ages)
## [1] "numeric"
names(d)
## [1] "kids" "ages"
d[1,]
## kids ages ## 1 Jack 12
class(d[1,])
## [1] "data.frame"
A = data.frame(x1 = rep(0,10), x2 = rep('b',10)) B = data.frame(x3 = rep(1,10), x2 = rep('d',10)) AB = cbind(A,B) head(AB)
## x1 x2 x3 x2 ## 1 0 b 1 d ## 2 0 b 1 d ## 3 0 b 1 d ## 4 0 b 1 d ## 5 0 b 1 d ## 6 0 b 1 d