R: Simplifying long ifelse statement

前端 未结 2 446
小蘑菇
小蘑菇 2021-01-25 15:04

I\'m trying to create new variables based on a procedure code variable with 2500+ values in a medical dataset to pull out the antibiotics, their dose, and route. I\'ve been abl

2条回答
  •  不知归路
    2021-01-25 16:04

    Two alternative methods, both using merges/joins. One advantage of this approach is that it is much easier to maintain: you have well-structured and manageable tables of procedures instead of (potentially really-long) lines of code with your ifelse statement. The comments suggesting %in% also reduce this problem, though you'll deal with manageable vectors instead of mangeable frames.

    Fake data:

    library(dplyr)
    library(tidyr)
    vet <- data_frame(ProcedureCode = c('6160', '2028', '2029'))
    
    1. One frame per procedure type. This is manageable, but might be annoying if you have a lot of different types. Repeat the left_join for each type.

      abs <- data_frame(ab=TRUE, ProcedureCode = c('6160', '2028'))
      antis <- data_frame(antibiotic=TRUE, ProcedureCode = c('2029'))
      vet %>%
        left_join(abs, by = "ProcedureCode") %>%
        left_join(antis, by = "ProcedureCode") %>%
        mutate_at(vars(ab, antibiotic), funs(!is.na(.)))
      # # A tibble: 3 × 3
      #   ProcedureCode    ab antibiotic
      #                  
      # 1          6160  TRUE      FALSE
      # 2          2028  TRUE      FALSE
      # 3          2029 FALSE       TRUE
      

      The use of ab=TRUE (etc) is so that there is a column to merge. The rows that do not match will have an NA, which mandates the need for !is.na(.) to convert T,NA,T to T,F,T.

      You could even use vectors of procedure codes instead, something like:

      vet %>%
        left_join(data_frame(ab=TRUE, ProcedureCode=vector_of_abs), by = "ProcedureCode") %>%
        ...
      

      Though that really only helps if you already have the codes as vectors, otherwise it seems to be solely whichever is easier for you to maintain.

    2. One frame with all procedures, requiring only a single frame for types and a single left_join.

      procedures <- tibble::tribble(
        ~ProcedureCode, ~procedure,
        '6160'        , 'ab',
        '2028'        , 'ab',
        '2029'        , 'antibiotic'
      )
      left_join(vet, procedures, by = "ProcedureCode")
      # # A tibble: 3 × 2
      #   ProcedureCode  procedure
      #                 
      # 1          6160         ab
      # 2          2028         ab
      # 3          2029 antibiotic
      

      You can either keep it as-is (if it makes sense to store it that way) or spread it to be like the others:

      left_join(vet, procedures, by = "ProcedureCode") %>%
        mutate(ignore=TRUE) %>%
        spread(procedure, ignore) %>%
        mutate_at(vars(ab, antibiotic), funs(!is.na(.)))
      # # A tibble: 3 × 3
      #   ProcedureCode    ab antibiotic
      #                  
      # 1          2028  TRUE      FALSE
      # 2          2029 FALSE       TRUE
      # 3          6160  TRUE      FALSE
      

      (Order after the join/merge is different here, but the data remains the same.)

    (I used logicals, it's easy enough to convert them to 1s and 0s, perhaps mutate(ab=1L*ab) or mutate(ab=as.integer(ab)).)

提交回复
热议问题