How to use mutate and ifelse to create new column with binary data

I have a table "AllProc" that have a unique identifier for every patient (Case.Index.Id) and a variety of resp support each patient received during their admission (BiPAP, etc), 1 = received, 0 = did not receive.

table1

I want to add two new columns to this data table, creating a master table "AllProcMaster". One column "NI_TX" for patients who only received "Non-Invasive" resp support (BiPAP, CPAP), and another column "NO_TX" for patients who received neither non-invasive or invasive support (Intubation). In my actual dataset, I have many more resp procedures, which is why I can't just isolate only "O2" for patients in this "NO_TX" column. This is the code I made, which did not work!

R
NI_RespPro <- c("BiPAP", "BiPAP", "CPAP")
Invasive_RespPro <- c("Intubation")
NIandIN_RespPro<- c("BiPAP", "CPAP", "Intubation")

AllProcMaster <- AllProc %>%
mutate(NI_TX = ifelse(all_of(NI_RespPro), 1, 0, group_by(Case.Index.Id))) %>%
mutate(No_TX = ifelse(all_of(NIandIN_RespPro) = 0, 1, group_by(CAse.Index.Id)))

Please help!

I am not sure exactly what your desired result is. Is this it?

All_Proc <- data.frame(Case.Index.Id=c("1a","1b","1c","2a","2b","2c"),
                       BiPAP=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))
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
AllProcMaster <- All_Proc %>% rowwise() %>% 
  mutate(NI_TX=BiPAP*CPAP,No_TX=BiPAP*CPAP*Intubation)
AllProcMaster
#> # A tibble: 6 x 7
#> # Rowwise: 
#>   Case.Index.Id BiPAP  CPAP Intubation    O2 NI_TX No_TX
#>   <chr>         <dbl> <dbl>      <dbl> <dbl> <dbl> <dbl>
#> 1 1a                1     1          0     0     1     0
#> 2 1b                0     0          0     0     0     0
#> 3 1c                1     0          1     1     0     0
#> 4 2a                0     0          0     1     0     0
#> 5 2b                0     1          0     1     0     0
#> 6 2c                1     1          1     1     1     1

Created on 2020-11-06 by the reprex package (v0.3.0)

What you have is very close to what I need! Except that the "No_TX" column, the only patient who received no treatment (mean NO bipap, cpap, or intubations) would be patient 1b and 2a (only 1b and 2a should have a "1" in this column"). How would I do this? Thank you so much for your help so far!

Like this?

AllProcMaster <- All_Proc %>% rowwise() %>% 
   mutate(NI_TX=BiPAP*CPAP,
          No_TX= as.integer((BiPAP + CPAP + Intubation) == 0))

AllProcMaster

  Case.Index.Id BiPAP  CPAP Intubation    O2 NI_TX No_TX
  <chr>         <dbl> <dbl>      <dbl> <dbl> <dbl> <int>
1 1a                1     1          0     0     1     0
2 1b                0     0          0     0     0     1
3 1c                1     0          1     1     0     0
4 2a                0     0          0     1     0     1
5 2b                0     1          0     1     0     0
6 2c                1     1          1     1     1     0

Thank you!!! That is it!

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.