Introduction
Like in Excel, data can be of different types.
Data types
Numeric (double) |
<dbl> |
Floating point number |
3.14159 |
Character |
<char> |
Character (text) string |
"S1P d18:1", "BQC" |
Logical |
<lgl> |
True or False, 1 or 0 |
TRUE; FALSE |
Integer |
<int> |
Number without digits |
3L; 7011L; -13L |
Factor |
<fct> |
Categorical data |
BQC; TQC; SPL |
Date |
<dt> |
Date |
2022-01-31 |
Time |
<tm> |
Time |
13:15; 1:15 pm |
Datetime |
<dttm> |
Date + Time |
2022-01-31 13:15 |
Type Conversions
to Number |
as.numeric() |
as.numeric("3.14") |
to Text |
as.character() |
as.character(3.14) |
to Factor |
factor()as.factor()forcats::as_factor() |
as.factor(c("TQC", "SPL", "TQC")factor( x = c("TQC", "SPL", "TQC"), levels = c("TQC", "SPL" ) )
|
to DateTime |
lubridate package |
lubridate::as_datetime("31-12-2022 13:15") |
Data structures
Vector |
Vector (series) of values. All values are of the same data type (see below) |
c(1,2,3,4)c("TQC", "BQC", "SPL")c(TRUE, FALSE) |
Matrix |
2-dimensional set of values. All values are of the same data type |
matrix(data = c(1:2),nrow = 2,ncol = 3)matrix(data = c("BQC", "SPL"),nrow = 2,ncol = 3) |
Data Frame
Tibble |
Table with columns that can have different data types |
tibble( No = c(1,2,3), Sample = c("A", "B", "C")) |
List |
Series of objects, that can be of different types, including e.g. tables |
list( Studysite = c("NUH", "SGH"), Cohort = tibble(No = c(1,2), Size = c(110,332))) |