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 structures
1 d
Atomic vector
List
2 d
Matrix
Data frame
3 d
Array
Atomic vectors
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
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
num [1:3, 1:2, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
Lists
l <- list (2 L, 3 , c (2 , 1 ), FALSE , "string" )
l
[[1]]
[1] 2
[[2]]
[1] 3
[[3]]
[1] 2 1
[[4]]
[1] FALSE
[[5]]
[1] "string"
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
'data.frame': 3 obs. of 2 variables:
$ country: chr "Canada" "USA" "Mexico"
$ var : num 2.9 3.1 4.5