R Programming for Data Science, by Roger D. Peng
x <- c(1, 2, 3.4)
x
[1] 1.0 2.0 3.4
y <- c (1,2, 3.4567)
y
[1] 1.0000 2.0000 3.4567
z <- c (TRUE, T, F)
z
[1] TRUE TRUE FALSE
class(z)
[1] "logical"
class (x)
[1] "numeric"
r <- c(1+0i,2+3i, 4)
r
[1] 1+0i 2+3i 4+0i
-21/0
[1] -Inf
0/0
[1] NaN
class(NaN)
[1] "numeric"
1+Inf
[1] Inf
Inf-Inf
[1] NaN
0^0
[1] 1
Inf * -Inf
[1] -Inf
c (1,2,3, 4, 5, "a")
[1] "1" "2" "3" "4" "5" "a"
c(TRUE, 0, 1, 0, -1)
[1] 1 0 1 0 -1
c(1,2,3,1+2i)
[1] 1+0i 2+0i 3+0i 1+2i
as.character(x)
[1] "1" "2" "3.4"
-1:5
[1] -1 0 1 2 3 4 5
as.logical(-1:5)
[1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE
as.logical(1.2)
[1] TRUE
as.logical("TRUE")
[1] TRUE
as.logical ("acb")
[1] NA
x
[1] 1.0 2.0 3.4
as.complex(x)
[1] 1.0+0i 2.0+0i 3.4+0i
x <- 1:5
y <- 6:10
z <- c (-4,0,3,12,5)
M <- rbind (x,y,z)
M
[,1] [,2] [,3] [,4] [,5]
x 1 2 3 4 5
y 6 7 8 9 10
z -4 0 3 12 5
dimnames (M) [[2]] <- c("col1", "col2", "col3", "col4", "col5")
dimnames (M) [[1]] <- c("row1", "row2", "row3")
attributes(M)
$dim
[1] 3 5
$dimnames
$dimnames[[1]]
[1] "row1" "row2" "row3"
$dimnames[[2]]
[1] "col1" "col2" "col3" "col4" "col5"
M
col1 col2 col3 col4 col5
row1 1 2 3 4 5
row2 6 7 8 9 10
row3 -4 0 3 12 5
class(M)
[1] "matrix" "array"
typeof(M)
[1] "double"
dimnames(M)
[[1]]
[1] "row1" "row2" "row3"
[[2]]
[1] "col1" "col2" "col3" "col4" "col5"
#Lists
x1 <- 1:3 #vector of integers
x2 <- list (1L, 3.4, TRUE) #list
x3 <- 1+3i #vector of complex of length 1
length(x1)
[1] 3
length(x2)
[1] 3
length(x3)
[1] 1
list (x1,x2,x3)
[[1]]
[1] 1 2 3
[[2]]
[[2]][[1]]
[1] 1
[[2]][[2]]
[1] 3.4
[[2]][[3]]
[1] TRUE
[[3]]
[1] 1+3i
list(x1,x2,M)
[[1]]
[1] 1 2 3
[[2]]
[[2]][[1]]
[1] 1
[[2]][[2]]
[1] 3.4
[[2]][[3]]
[1] TRUE
[[3]]
col1 col2 col3 col4 col5
row1 1 2 3 4 5
row2 6 7 8 9 10
row3 -4 0 3 12 5
A <- array(1:24, dim= c(2,3,4))
dim(A)
[1] 2 3 4
attributes(A)
$dim
[1] 2 3 4
class(NaN)
[1] "numeric"
1+NaN
[1] NaN
Inf-Inf
[1] NaN
-3 * Inf
[1] -Inf
Inf+NaN
[1] NaN
0*NaN
[1] NaN
NaN/NaN
[1] NaN