how to mutate new column with data from strings in other columns

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)

suppressPackageStartupMessages({
  library(dplyr)
})

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)
)

AllProc_sample %>% mutate(
  Noninvasive = ifelse(BiPAP1 == 1 | BiPAP2 == 1 | CPAP == 1,1,0),
  NoTreat = ifelse(BiPAP1 == 0 & BiPAP2 == 0 & CPAP == 0 &
                   Intubation == 0,1,0)) %>%
  mutate(MaxTreat = case_when(
    NoTreat == 1 ~ 1,
    Intubation == 1 ~ 3,
    sum(BiPAP1 + BiPAP2 + CPAP) > 0 ~ 2,
  ))
#>   ID BiPAP1 CPAP Intubation O2 BiPAP2 Noninvasive NoTreat MaxTreat
#> 1  A      1    1          0  0      0           1       0        2
#> 2  B      0    0          0  0      0           0       1        1
#> 3  C      1    0          1  1      0           1       0        3
#> 4  D      0    0          0  1      1           1       0        2
#> 5  E      0    1          0  1      0           1       0        2
#> 6  F      1    1          1  1      0           1       0        3

Created on 2020-11-09 by the reprex package (v0.3.0.9001)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thanks so much, this was perfect :slight_smile:

1 Like