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
# c() concatenates arguments to create a vector
x = c(4, 3, 3, 4, 3, 1)
x
length(x)
2 * x + 1 # element-wise arithmetic
# Boolean vector (default is FALSE)
y = vector(mode = "logical", length = 4)
y
# numeric vector (default is 0)
z = vector(length = 3, mode = "numeric")
z
q = rep(3.2, times = 10) # repeat value multiple times
q
w = seq(0, 1, by = 0.1) # values in [0,1] in 0.1 increments
w
# 11 evenly spaced numbers between 0 and 1
w = seq(0, 1, length.out = 11)
w
# create an array of booleans reflecting whether condition holds
w <= 0.5
any(w <= 0.5) # is it true for some elements?
all(w <= 0.5) # is it true for all elements?
which(w <= 0.5) # for which elements is it true?
w[w <= 0.5] # extracting from w entries for which w<=0.5
subset(w, w <= 0.5) # an alternative with the subset function
w[w <= 0.5] = 0 # zero out all components smaller or equal to 0.5
w
# Arrays are multidimensional generalizations of vectors
z = seq(1, 20,length.out = 20) # create a vector 1, 2, ..., 20
x = array(data = z, dim = c(4, 5)) # create a 2-d array
x
x[2, 3] # refer to the second row and third column
x[2, ] # refer to the entire second row
x[-1, ] # all but the first row - same as x[c(2, 3, 4), ]
y = x[c(1, 2), c(1, 2)] # 2x2 top left sub-matrix
2 * y + 1 # element-wise operation
y %*% y # matrix product (both arguments are matrices)
# inner product (both vectors have the same dimensions)
x[1, ] %*% x[1, ]
t(x) # matrix transpose
outer(x[, 1], x[, 1]) # outer product
rbind(x[1, ], x[1, ]) # vertical concatenation
cbind(x[1, ], x[1, ]) # horizontal concatenation
# Multidimensional arrays
m = matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
m
m[3] # counting by columns A[3] = A[1, 2]
# Lists are ordered collections which permit positions to hold variables
Enter to Rename, Shift+Enter to Preview