-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.qmd
64 lines (54 loc) · 5.8 KB
/
tables.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Data Formats and Types in R
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
```
## Introduction
Like in Excel, data can be of different types.
## 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` |
+------------------+---------------+-------------------------+----------------------+
## 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")` |
+-------------+-------------------------------------------+---------------------------------------------------------------------------------------------------------+
## 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\ | Table with columns that can have different data types | `tibble(No = c(1,2,3), Sample = c("A", "B", "C"))` |
| Tibble | | |
+-------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------------------+
| 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)))` |
+-------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------------------+
##