3 min read

Pipe feature*

The pipe feature

The pipe feature is an alternative syntax that many programmers find makes their work more efficient. It is explained in the textbook on p. 34.

library(dplyr)

#read in our usual heart data
heart <- read.csv("C:/epi551/heart.csv", header=T)
head(heart)
##   Age Sex Chest_Pain_Type Resting_Blood_Pressure Serum_Cholesterol
## 1  63   1               1                    145               233
## 2  67   1               4                    160               286
## 3  67   1               4                    120               229
## 4  37   1               3                    130               250
## 5  41   0               2                    130               204
## 6  56   1               2                    120               236
##   Fasting_Blood_Sugar Resting_ECG Max_Heart_Rate_Achieved
## 1                   1           2                     150
## 2                   0           2                     108
## 3                   0           2                     129
## 4                   0           0                     187
## 5                   0           2                     172
## 6                   0           0                     178
##   Exercise_Induced_Angina ST_Depression_Exercise Peak_Exercise_ST_Segment
## 1                       0                    2.3                        3
## 2                       1                    1.5                        2
## 3                       1                    2.6                        2
## 4                       0                    3.5                        3
## 5                       0                    1.4                        1
## 6                       0                    0.8                        1
##   Num_Major_Vessels_Flouro Thalassemia Diagnosis_Heart_Disease diagnosis
## 1                      0.0         6.0                       0        No
## 2                      3.0         3.0                       2       Yes
## 3                      2.0         7.0                       1       Yes
## 4                      0.0         3.0                       0        No
## 5                      0.0         3.0                       0        No
## 6                      0.0         3.0                       0        No
#make some changes to the data in three steps
heart2 <- rename(heart,thal=Thalassemia)
heart3 <- mutate(heart2,agemo=Age*12)
heart4 <- filter(heart3,Sex==1)

head(heart4)
##   Age Sex Chest_Pain_Type Resting_Blood_Pressure Serum_Cholesterol
## 1  63   1               1                    145               233
## 2  67   1               4                    160               286
## 3  67   1               4                    120               229
## 4  37   1               3                    130               250
## 5  56   1               2                    120               236
## 6  63   1               4                    130               254
##   Fasting_Blood_Sugar Resting_ECG Max_Heart_Rate_Achieved
## 1                   1           2                     150
## 2                   0           2                     108
## 3                   0           2                     129
## 4                   0           0                     187
## 5                   0           0                     178
## 6                   0           2                     147
##   Exercise_Induced_Angina ST_Depression_Exercise Peak_Exercise_ST_Segment
## 1                       0                    2.3                        3
## 2                       1                    1.5                        2
## 3                       1                    2.6                        2
## 4                       0                    3.5                        3
## 5                       0                    0.8                        1
## 6                       0                    1.4                        2
##   Num_Major_Vessels_Flouro thal Diagnosis_Heart_Disease diagnosis agemo
## 1                      0.0  6.0                       0        No   756
## 2                      3.0  3.0                       2       Yes   804
## 3                      2.0  7.0                       1       Yes   804
## 4                      0.0  3.0                       0        No   444
## 5                      0.0  3.0                       0        No   672
## 6                      1.0  7.0                       2       Yes   756
#make the same changes to the data in one step using %>%
heartpipe <- heart %>% rename(thal=Thalassemia) %>% mutate(agemo=Age*12) %>%
  filter(Sex==1)

head(heartpipe)
##   Age Sex Chest_Pain_Type Resting_Blood_Pressure Serum_Cholesterol
## 1  63   1               1                    145               233
## 2  67   1               4                    160               286
## 3  67   1               4                    120               229
## 4  37   1               3                    130               250
## 5  56   1               2                    120               236
## 6  63   1               4                    130               254
##   Fasting_Blood_Sugar Resting_ECG Max_Heart_Rate_Achieved
## 1                   1           2                     150
## 2                   0           2                     108
## 3                   0           2                     129
## 4                   0           0                     187
## 5                   0           0                     178
## 6                   0           2                     147
##   Exercise_Induced_Angina ST_Depression_Exercise Peak_Exercise_ST_Segment
## 1                       0                    2.3                        3
## 2                       1                    1.5                        2
## 3                       1                    2.6                        2
## 4                       0                    3.5                        3
## 5                       0                    0.8                        1
## 6                       0                    1.4                        2
##   Num_Major_Vessels_Flouro thal Diagnosis_Heart_Disease diagnosis agemo
## 1                      0.0  6.0                       0        No   756
## 2                      3.0  3.0                       2       Yes   804
## 3                      2.0  7.0                       1       Yes   804
## 4                      0.0  3.0                       0        No   444
## 5                      0.0  3.0                       0        No   672
## 6                      1.0  7.0                       2       Yes   756