forked from joseph-ProCogia/SAS-to-R-for-Medicine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipe Syntax.R
43 lines (27 loc) · 963 Bytes
/
Pipe Syntax.R
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
#SAS to R - Introduction Course
#Introduction - Pipe Syntax
#ProCogia - Higher Intelligence. Deeper Insights. Smarter Decisions.
#add tidyverse reference for table1
library(tidyverse)
data("table1")
head(table1)
#filter, then mutate, then arrange
tb_summary <- arrange(
mutate(
filter(
table1, country %in% c("Afghanistan", "China")
), incidence=100000*cases/population),
-incidence
)
#output
tb_summary
#piping syntax
table1 %>% #start with WHO data
filter(country %in% c("Afghanistan", "China")) %>% #AND THEN filter
mutate(incidence=100000*cases/population) %>% #AND THEN mutate
arrange(-incidence) #AND THEN sort with arrange()
#Exercise 3.1
#use the filter() function to filter table1 down to countries Brazil and China
#AND THEN apply the mutate function to get the incidence formula used above
#AND THEN sort data in ascending order using arrange()