5  Data Formats and Types in R

5.1 Introduction

Like in Excel, data can be of different types.

5.2 Important R data types

Type Short Description Example(s)
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

5.3 Converting Data Types

Type Functions Example(s)
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")

5.4 Important R data formats

Type Description Example(s)
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)))

5.5