rsapply

Efficiently replacing multiple values in a column


I have a table which looks like this, where I have multiple values in two identical columns which must be replaced in one column like so:

Before

Column_A Column_B
Default1 Default 1
Default2 Default 2
Default3 Default 3
Default4 Default 4
Default5 Default 5
Default6 Default 6

After

Column_A Column_B
Default1 Q10
Default2 Q10
Default3 Q10
Default4 B12
Default5 B12
Default6 B12

My issue is that the table is 135 rows long, and I don't want to use this:

Data.dat$Column_B[Data.dat$Column_B == "default55"] <- "Q10"

a million times.

What is the most efficient way I could accomplish this goal?

I have tried individual replace functions, which work well...but aren't efficient.

I also tried sapply functions (in this video here), but all of the values are replaced in both columns.

If I didn't have to replace only one column, the sapply function would be perfect!

I've tried recode, but get a operator is invalid for atomic vectors error which I can't seem to solve.

Example recode below:

Data.dat$Column_B<-recode(Data.dat$Column_B, "c('default1','default2','default3','default4','default5','default6','default7')='Q10'")


Solution

  • How about case_match of dplyr?

    Data example:

    df <- data.frame(
      Column_A = c("Default1", "Default2", "Default3", "Default4", "Default5", "Default6"),
      Column_B = c("Default 1", "Default 2", "Default 3", "Default 4", "Default 5", "Default 6")
    )
    

    Use case_match

    library(dplyr)
    df_new <- df %>%
      mutate(Column_B = case_match(
        Column_B,
        c("Default 1", "Default 2", "Default 3") ~ "Q10",
        c("Default 4", "Default 5", "Default 6") ~ "B12"
      ))
    
    df_new
    
    > df_new
      Column_A Column_B
    1 Default1      Q10
    2 Default2      Q10
    3 Default3      Q10
    4 Default4      B12
    5 Default5      B12
    6 Default6      B12