I have a table "AllProc_sample" that lists all respiratory support that a patient received during their admission. I want to create three new columns--one named "NonInvasive" for all patients who received non-invasive treatment (BiPAP1 + CPAP + BiPAP2). A second column "NoTreat" for all patients who received NEITHER non-invasive treatment or intubations (BiPAP1 + CPAP + BiPAP2 + Intubation). Finally a third column "MaxTreat" where patients who received no treatment (the ones singled out in "NoTreat") gets a "1", patients who ONLY gots non-invasive (BiPAP etc, but NO intubations) gets a "2", and patients who were intubated (anyone who has a 1 in "Intubation" gets a "3". I am very new to R!
AllProc_sample <- data.frame(ID = c("A","B","C","D","E","F"),
BiPAP1=c(1,0,1,0,0,1),
CPAP=c(1,0,0,0,1,1),
Intubation=c(0,0,1,0,0,1),
O2=c(0,0,1,1,1,1),
BiPAP2=c(0,0,0,1,0,0))
The code I tried is this--it will not work because of an unexpected symbol?:
AllProc_Master <- AllProc_sample %>%
mutate(NonIn_Treat = BiPAP1BiPAP2CPAP)%>%
mutate(No_Treat = as.integer((BiPAP1+BiPAP2+CPAP+Intubation) == 0)
What I want is a table that looks like this:
library(reprex)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
AllProc_Master <- data.frame(ID = c("A","B","C","D","E","F"),
BiPAP1=c(1,0,1,0,0,1),
CPAP=c(1,0,0,0,1,1),
Intubation=c(0,0,1,0,0,1),
O2=c(0,0,1,1,1,1),
BiPAP2=c(0,0,0,1,0,0),
NonInvasive=c(1,0,1,1,1,1),
NoTreat=c(0,1,0,0,0,0),
MaxTreat=c(2,1,3,2,2,3))
Created on 2020-11-09 by the reprex package (v0.3.0)