-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsession-objects.qmd
79 lines (54 loc) · 1.81 KB
/
session-objects.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
---
title: "Introduction to R and Rstudio"
subtitle: "Session - objects"
execute:
eval: true
---
## Creating an object
```{r}
#| label: "libs"
#| include: false
library(readr)
library(dplyr)
```
```{r }
#| label: "load-data"
#| echo: 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)
```
Objects that are created from imported data appear in the Environment pane
It is possible to also create objects from code
## Temporary and temporary
The following code exists only as the code is run and the underlying object `beds_data` is never changed
```{r}
beds_data |>
summarise(total_beds = sum(beds_av, na.rm = TRUE),
total_occupancy = sum(occ_av, na.rm = TRUE),
.by = org_name) |>
mutate(perc_occ = total_occupancy / total_beds) |>
arrange(desc(perc_occ))
```
## Saving as an object
In R the assign operator `<-` is conventionally used instead of `=` although that will work
Shortcut key is `Alt and -`
```{r}
bed_occupancy <- beds_data |>
summarise(total_beds = sum(beds_av, na.rm = TRUE),
total_occupancy = sum(occ_av, na.rm = TRUE),
.by = org_name) |>
mutate(perc_occ = total_occupancy / total_beds) |>
arrange(desc(perc_occ))
```
## Naming style
The way names are written out is a question of style but it's best to be consistent.
Other ways of writing names will work but are best avoided like `ALLCAPS` and `With Spaces`
```{r}
#| eval: false
camelCase # first letter is small case
PascalCase # every letter is capital
snake_case # lower case and words are separated with underline
kebab-case # lower case and hyphen, used in RMarkdown but not R scripts
```
## End session