3

I have the following code and normally use ifelse to create a column with my renamed values in my data.frame, but this time my number of nested values >50 so does anyone have any ideas on what else I can use to create a new column based on the values in another? I event tried splitting them over 3 new columns and then combining those new columns but kept getting errors.

df$ARV <- transform(df$ADAV1 == "055", "Isoprinosine",
             ifelse(df$ADAV1 == "056", "HPA-23",
                    ifelse(df$ADAV1 == "057", "Suramin",
                           ifelse(df$ADAV1 == "058", "Ribavirin",
                                  ifelse(df$ADAV1 == "090", "Alpha Interferon",
                                         ifelse(df$ADAV1 == "091", "Foscarnet/PFA",
                                                ifelse(df$ADAV1 == "092", "AZT",
                                                       ifelse(df$ADAV1 == "094", "ddC",
                                                              ifelse(df$ADAV1 == "098", "AL-721",
                                                                     ifelse(df$ADAV1 == "101", "Ampligen",
      ifelse(df$ADAV1 == "108", "Peptide T",
       ifelse(df$ADAV1 == "110", "Dextran-Sulfate",
         ifelse(df$ADAV1 == "122", "Beta Interferon",
           ifelse(df$ADAV1 == "128", "CD4",
             ifelse(df$ADAV1 == "147", "ddI",
               ifelse(df$ADAV1 == "159", "d4T",
                 ifelse(df$ADAV1 == "163", "ddA",
                   ifelse(df$ADAV1 == "179", "adenosine arabinoside",
                     ifelse(df$ADAV1 == "180", "AZT/ddI Blinded Trial",
                      ifelse(df$ADAV1 == "185", "AZT/ddC Blinded Trial",
                       ifelse(df$ADAV1 == "186", "ddI/ddC Blinded Trial",
                        ifelse(df$ADAV1 == "187", "AZT/ddI/ddC Blinded Trial",
                         ifelse(df$ADAV1 == "191", "Nevirapine",
                           ifelse(df$ADAV1 == "192", "TAT inhibitors",
                             ifelse(df$ADAV1 == "193", "PI-O",
                               ifelse(df$ADAV1 == "194", "U-90/152/Delavirdine",
                                 ifelse(df$ADAV1 == "201", "AZT/d4T Trial",
                                   ifelse(df$ADAV1 == "204", "3TC",
                                     ifelse(df$ADAV1 == "205", "AZT/3-TC Blinded Trial",
                        ifelse(df$ADAV1 == "206", "AZT/ddI/PI Blinded Trial",
                               NA )))))))))))))))))))))))))))))))
df$ARV2  <- ifelse(df$ADAV1 == "208", "AZT/PI Blinded Trial",
         ifelse(df$ADAV1 == "209", "d4T/PI Blinded Trial",
           ifelse(df$ADAV1 == "210", "Saquinavir",
             ifelse(df$ADAV1 == "211", "Ritonavir",
                ifelse(df$ADAV1 == "212", "Indinavir",
                  ifelse(df$ADAV1 == "214", "AZT/3-TC/PI Blinded Trial",
                    ifelse(df$ADAV1 == "216", "Nelfinavir",
                       ifelse(df$ADAV1 == "217", "ABT-378",
                         ifelse(df$ADAV1 == "218", "Abacavir",
                  ifelse(df$ADAV1 == "219", "141W94",
                             ifelse(df$ADAV1 == "220", "Enfavirenz",
                                     ifelse(df$ADAV1 == "221", "MKC442",
                                       ifelse(df$ADAV1 == "222", "Lobucavir",
                                         ifelse(df$ADAV1 == "223", "Loviride",
                                           ifelse(df$ADAV1 == "224", "Adefovir",
                                             ifelse(df$ADAV1 == "227", "Combivir",
                                               ifelse(df$ADAV1 == "231", "Vistide",
                                                 ifelse(df$ADAV1 == "233", "T-20",
                                                   ifelse(df$ADAV1 == "234", "PMPA",
                                                      ifelse(df$ADAV1 == "238", "Timpranavir",
                                                        ifelse(df$ADAV1 == "239", "Emtricitabine",
                                                          ifelse(df$ADAV1 == "998", "Other",
                                                             NA ))))))))))))))))))))))
A.D.
  • 61
  • 4
  • 7
    Just create a key/value data.frame and then do a join i.e. `keyval <- data.frame(ADAV1 = c("055", "056"), val = c("Isoprinosine",...)); left_join(df, keyval)` – akrun May 07 '18 at 15:45
  • This might help: https://stackoverflow.com/a/50179954/3358272 – r2evans May 07 '18 at 15:47
  • 1
    Expanding on akrun's comment, you'd create a table with a "key" column ADAV1 that you are mapping from; another column for the corresponding ARV you want; and another for the ARV2. Then join to get those in the main DF. (Btw, @akrun's left_join uses the dplyr package.) – Frank May 07 '18 at 15:48

1 Answers1

3

The easiest option would be to create a key/val data.frame and then do a left_join

library(dplyr)
keyval <- data.frame(ADAV1 = c("055", "056", ...),
             val = c("Isoprinosine","HPA-23", ...), stringsAsFactors = FALSE)

left_join(df, keyval) %>% 
            mutate(ARV = replace(ADAV1, is.na(ADAV1), val))  

Similarly, if there is another set of key/val for generating 'ARV2', then create

keyval2 <- data.frame(ADAV1 = c("208", "209", ...),
             val = c( "AZT/PI Blinded Trial", "d4T/PI Blinded Trial", ...),
       stringsAsFactors = FALSE)

and do the second join


Or place them in a list and then use map

library(purrr)
list(keyval, keyval2) %>%
            map2_df(., c("ARV", "ARV2"), ~ left_join(df, .x) %>%
                    transmute(!! (.y) := replace(ADAV1, is.na(ADAV1), val))) %>%
    bind_cols(df, .) 
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @A.D. I used the `...` to fill in those key/vals. – akrun May 07 '18 at 16:14
  • 1
    OP could also edit a csv with the key-val pairs; might be easier to maintain than `data.frame(k = long_expression, v = longer_expression)` @A.D. – Frank May 07 '18 at 16:17