Home > Software engineering >  paste string to value based on column condition
paste string to value based on column condition

Time:10-06

I have a dataframe that looks like this:

df <- tibble::tribble(
  ~subcateg, ~names, ~categ, ~names2,
  "A00", "Kidney failure", "A00", "Kidney failure",
  "A001", "Kidney failure reason1", "A00", "Kidney failure",
  "A002", "Kidney failure reason2","A00", "Kidney failure",
  "A003", "Kidney failure reason3","A00", "Kidney failure",
  "B00", "Heart failure", "B00", "Heart failure",
  "B001", "Heart failure reason1",  "B00", "Heart failure",
  "B002", "Heart failure reason2",  "B00", "Heart failure",
  "B003", "Heart failure reason3",  "B00", "Heart failure",
  "C00", "Lung failure", "C00", "Lung failure",
  "C001", "Lung failure reason1",  "C00", "Lung failure",
  "C002", "Lung failure reason2",  "C00", "Lung failure",
  "C003", "Lung failure reason3",  "C00", "Lung failure",
)

And I need to add an "X" to the values of column 1 that have only 3 chr. This is, the values where columns subcateg=categ, so it should look like this:

    df <- tibble::tribble(
  ~subcateg, ~names, ~categ, ~names2,
  "A00X", "Kidney failure", "A00", "Kidney failure",
  "A001", "Kidney failure reason1", "A00", "Kidney failure",
  "A002", "Kidney failure reason2","A00", "Kidney failure",
  "A003", "Kidney failure reason3","A00", "Kidney failure",
  "B00X", "Heart failure", "B00", "Heart failure",
  "B001", "Heart failure reason1",  "B00", "Heart failure",
  "B002", "Heart failure reason2",  "B00", "Heart failure",
  "B003", "Heart failure reason3",  "B00", "Heart failure",
  "C00X", "Lung failure", "C00", "Lung failure",
  "C001", "Lung failure reason1",  "C00", "Lung failure",
  "C002", "Lung failure reason2",  "C00", "Lung failure",
  "C003", "Lung failure reason3",  "C00", "Lung failure",
)

I've tried something like this:

df %>%
  filter(subcateg = categ)  %>%
  paste0(df$subcateg, "X")

but it doesn't work. Any ideas?

Thanks!

CodePudding user response:

We could use nchar in an ifelse statement:


library(dplyr)
df %>% 
  mutate(subcateg= ifelse(nchar(subcateg)==3, paste0(subcateg, "X"), subcateg))
   subcateg names                  categ names2        
   <chr>    <chr>                  <chr> <chr>         
 1 A00X     Kidney failure         A00   Kidney failure
 2 A001     Kidney failure reason1 A00   Kidney failure
 3 A002     Kidney failure reason2 A00   Kidney failure
 4 A003     Kidney failure reason3 A00   Kidney failure
 5 B00X     Heart failure          B00   Heart failure 
 6 B001     Heart failure reason1  B00   Heart failure 
 7 B002     Heart failure reason2  B00   Heart failure 
 8 B003     Heart failure reason3  B00   Heart failure 
 9 C00X     Lung failure           C00   Lung failure  
10 C001     Lung failure reason1   C00   Lung failure  
11 C002     Lung failure reason2   C00   Lung failure  
12 C003     Lung failure reason3   C00   Lung failure 

CodePudding user response:

We may use case_when to create a logical vector based on the nchar value of 3 in 'subcateg' to paste (str_c) the 'X' at the end of 'subcateg'

library(dplyr)
library(stringr)
df %>% 
   mutate(subcateg = case_when(nchar(subcateg) == 3 ~ 
         str_c(subcateg, 'X'), TRUE ~ subcateg))

-ouptut

# A tibble: 12 × 4
   subcateg names                  categ names2        
   <chr>    <chr>                  <chr> <chr>         
 1 A00X     Kidney failure         A00   Kidney failure
 2 A001     Kidney failure reason1 A00   Kidney failure
 3 A002     Kidney failure reason2 A00   Kidney failure
 4 A003     Kidney failure reason3 A00   Kidney failure
 5 B00X     Heart failure          B00   Heart failure 
 6 B001     Heart failure reason1  B00   Heart failure 
 7 B002     Heart failure reason2  B00   Heart failure 
 8 B003     Heart failure reason3  B00   Heart failure 
 9 C00X     Lung failure           C00   Lung failure  
10 C001     Lung failure reason1   C00   Lung failure  
11 C002     Lung failure reason2   C00   Lung failure  
12 C003     Lung failure reason3   C00   Lung failure  
  • Related