Home > front end >  replacing multiple values seperated by a comma by other values
replacing multiple values seperated by a comma by other values

Time:11-09

here are a few rows of my data :

dput result :

    structure(list(A= c("2", "1, 2", "2", "2"), B= c("2", 
"none", "1", "1")), row.names = 10:13, class = "data.frame")

actual data :

              A            B 
10            2            2
11         1, 2         none
12            2            1
13            2            1

I'm trying to replace 1 and 2 by their other values depending on the column, for column A for instance, 1 would be replaced by 'currently' and 2 by 'in the past', so my new data would be :

              A                   B 
10        in the past             2
11        currently, in the past  none
12        in the past             1
13        in the past             1

I'm using mapvalues from plyr which works fine except for the cases where I have multiple values like (1,2) for the same row, in the case, (1,2) are unchanged . is there another way to solve this?

the mapvalues command :

dt$A = mapvalues(dt$A ,  old_values, new_values)

CodePudding user response:

Using stringi package:

stringi::stri_replace_all_fixed(
  df$A, 
  c("1", "2"), 
  c("currently", "in the past"), 
  vectorize_all = FALSE
)

CodePudding user response:

df %>% 
  na_if("none") %>%  
  mutate(A = A %>% str_replace_all("1", "currently") %>% 
                   str_replace_all("2", "in the past"))

# A tibble: 4 x 2
  A                      B    
  <chr>                  <chr>
1 in the past            2    
2 currently, in the past NA   
3 in the past            1    
4 in the past            1    
  • Related