-
Notifications
You must be signed in to change notification settings - Fork 4
/
session-vectors.qmd
88 lines (61 loc) · 1.57 KB
/
session-vectors.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: "Introduction to R and Rstudio"
subtitle: "Session - Vectors"
---
## Vectors
```{r}
#| echo: false
#| label: "libs"
#| include: false
library(readr)
library(dplyr)
```
```{r}
#| echo: false
#| label: "load-data"
#| eval: false
beds_data <- read_csv(url("https://raw.githubusercontent.com/nhs-r-community/intro_r_data/main/beds_data.csv"),
col_types = cols(date = col_date(format = "%d/%m/%Y")),
skip = 3)
```
Are a type of data format in R but may be familiar
You can create a vector with function: `c()` for `concatenate/combine`. In SQL it would just be `()` with no c
```{r}
c(100, 80, 200)
c("beds", "staff", "patients")
# Mixing strings (characters) and numeric values results in all being strings
c("beds", 80, "patients")
```
## Looking for more than one string
```{r}
c("Bradford District Care", "Bradford District Care Trust")
# can be made into an object
org_lookup <- c("Bradford District Care", "Bradford District Care Trust")
```
## Using in `filter()`
Filter by org_name IN the lookup list.
```{r }
#| code-line-numbers: "2"
beds_data |>
filter(org_name %in% org_lookup)
```
Code would look like this in SQL
```{sql}
SELECT *
FROM Table
WHERE Colm IN ('Bradford District Care', 'Bradford District Care Trust')
```
## Negative or NOT IN
Filter by org_name NOT IN the lookup list.
```{r }
#| code-line-numbers: "2"
beds_data |>
filter(!org_name %in% org_lookup)
```
In SQL
```{sql}
SELECT *
FROM Table
WHERE Colm NOT IN ('Bradford District Care', 'Bradford District Care Trust')
```
## End session