R: Simplifying long ifelse statement

徘徊边缘 提交于 2019-12-02 07:08:47

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
    #           <chr> <lgl>      <lgl>
    # 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
    #           <chr>      <chr>
    # 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
    #           <chr> <lgl>      <lgl>
    # 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)).)

A base R approach for a simple option:

# my dummy data
df1 <- data.frame("v1" = c(LETTERS[1:10]), "v2" = rep(NA, 10))

# step 1, fill the column with 0 (the else part of your code)
df1[,'v2'] <- 0

# step 2, create a vector containing ids you want to change
change_vec <- c("A", "C", "D", "F")

# step 3, use %in% to index and replace with 1
df1[,'v2'][df1[,'v1'] %in% change_vec] <- 1

In most cases this will be adequate, but be aware of the risks of using indexing vectors that contain numeric values.

https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!