R has many data structures includes vector, list, matrix, data frame, and array. This article introduces each of them.
From a high level, it can be summarized as follows:
For data objects, R provides a few functions to check their features. For example,
typeof()
— what data type is it?length()
— how long is it?attributes()
— does it have any metadata?
Atomic Vectors
Atomic vectors include six basic data types. They are
- character:
"hello"
,"goodbye"
- numeric (real or decimal):
1
,-0.5
- integer:
1L
(L denotes an integer. Why is “L” used as a suffix?) - logical:
TRUE
,FALSE
- complex:
1+2i
- raw: hold raw bytes
A vector requires all the members are in the same data type.
Lists
A list allows different data types, therefore, lists in R are also called generic vectors. You can create a list using list()
, and as.list()
to coerce an object to a list.
Matrix
Matrix is a special array. An array can store multiple dimensions of data, while a matrix…