<- 2
a a
[1] 2
typeof(a)
[1] "double"
str(a)
num 2
length(a)
[1] 1
dim(a)
NULL
Marie-Hélène Burle
This section covers the various data types and structures available in R.
Dimension | Homogeneous | Heterogeneous |
---|---|---|
1 d | Atomic vector | List |
2 d | Matrix | Data frame |
3 d | Array |
The dim
attribute of a vector doesn’t exist (hence the NULL
). This makes vectors different from one-dimensional arrays which have a dim
of 1
.
You might have noticed that 2
is a double (double precision floating point number, equivalent of “float” in other languages). In R, this is the default, even if you don’t type 2.0
. This prevents the kind of weirdness you can find in, for instance, Python.
In Python:
>>> 2 == 2.0
True
>>> type(2) == type(2.0)
False
>>> type(2)
<class 'int'>
>>> type(2.0)
<class 'float'>
In R:
> 2 == 2.0
[1] TRUE
> typeof(2) == typeof(2.0)
[1] TRUE
> typeof(2)
[1] "double"
> typeof(2.0)
[1] "double"
If you want to define an integer variable, you use:
There are six vector types:
[1] TRUE TRUE NA FALSE
[1] "logical"
logi [1:4] TRUE TRUE NA FALSE
NA
(“Not Available”) is a logical constant of length one. It is an indicator for a missing value.
Vectors are homogeneous, so all elements need to be of the same type.
If you use elements of different types, R will convert some of them to ensure that they become of the same type:
[1] "This is a string" "3" "test"
[1] "character"
chr [1:3] "This is a string" "3" "test"
[1] "string" "TRUE" "2" "3.1"
[1] "character"
chr [1:4] "string" "TRUE" "2" "3.1"
The binary operator :
is equivalent to the seq()
function and generates a regular sequence of integers:
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[1] "integer"
int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
[1] 12
[1] 3 4
The default is byrow = FALSE
. If you want the matrix to be filled in by row, you need to set this argument to TRUE
:
, , 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
[1] "double"
num [1:3, 1:2, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
[1] 24
[1] 3 2 4
[[1]]
[1] 2
[[2]]
[1] 3
[1] "list"
List of 2
$ : num 2
$ : num 3
[1] 2
NULL
As with atomic vectors, lists do not have a dim
attribute. Lists are in fact a different type of vectors.
Lists can be heterogeneous:
Data frames contain tabular data. Under the hood, a data frame is a list of vectors.