Data types and structures

Author

Marie-Hélène Burle

It might be time to talk a bit more formally about the various data types and structures available in R. The goal of this course is not to get bogged down in the nitty-gritty of R syntax, so this section is kept very short.

Data types

typeof("Some words")
[1] "character"
typeof(2)
[1] "double"
typeof(2.0)
[1] "double"
typeof(2L)
[1] "integer"
typeof(TRUE)
[1] "logical"

Data structures

Dimension Homogeneous Heterogeneous
1 d Atomic vector List
2 d Matrix Data frame
3 d Array

Atomic vectors

c(2, 4, 1)
[1] 2 4 1
str(c(2, 4, 1))
 num [1:3] 2 4 1
c(2.2, 4.4, 1.0)
[1] 2.2 4.4 1.0
str(c(2.2, 4.4, 1.0))
 num [1:3] 2.2 4.4 1
1:3
[1] 1 2 3
str(1:3)
 int [1:3] 1 2 3
c("some", "random", "words")
[1] "some"   "random" "words" 
str(c("some", "random", "words"))
 chr [1:3] "some" "random" "words"

Matrices

m <- matrix(1:12, nrow = 3, ncol = 4)
m
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
str(m)
 int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...

Arrays

a <- array(as.double(1:24), c(3, 2, 4))
a
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

, , 3

     [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18

, , 4

     [,1] [,2]
[1,]   19   22
[2,]   20   23
[3,]   21   24
str(a)
 num [1:3, 1:2, 1:4] 1 2 3 4 5 6 7 8 9 10 ...

Lists

l <- list(2L, 3, c(2, 1), FALSE, "string")
l
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 2 1

[[4]]
[1] FALSE

[[5]]
[1] "string"
str(l)
List of 5
 $ : int 2
 $ : num 3
 $ : num [1:2] 2 1
 $ : logi FALSE
 $ : chr "string"

Data frames

d <- data.frame(
  country = c("Canada", "USA", "Mexico"),
  var = c(2.9, 3.1, 4.5)
)
d
  country var
1  Canada 2.9
2     USA 3.1
3  Mexico 4.5
str(d)
'data.frame':   3 obs. of  2 variables:
 $ country: chr  "Canada" "USA" "Mexico"
 $ var    : num  2.9 3.1 4.5