Rename observations more than one criteria

I have a colum in a dataframe called “Inheritance” and the observations:

Autosomal dominant
Autosomal dominant, digenic
Autosomal dominant, autosomal recessive
Autosomal recessive
Autosomal recessive, digenic
X-linked autosomal
X-linked recessive

I want to rename the observations (to regroup into less groups) that contains “Autosomal dominant”, “Autosomal dominat, digenic”, "Autosomal dominat, autosomal recessive” to “AD”, regardeless if “Autosomal dominat” cames with another classification in the same observation. And “Autosomal recessive” to “AR”. Also, “X-linked autosomal” and “X-linked recessive” as “X”. How can I tell R to consider just a couple of words and ignoring the other ones?

Like this?

  {library(tibble) ;library(stringr) ;library(dplyr)}

df1 <- tibble::tribble(
  ~ Inheritance, ~ Important_Letter,
  "Autosomal dominant",  "a",
  "Autosomal dominant, digenic", "b",
  "Autosomal dominant, autosomal recessive",  "c",
  "Autosomal recessive", "d",
  "Autosomal recessive, digenic", "z",
  "X-linked autosomal",  "y",
  "X-linked recessive", "x",
  "Garden Pond",  "w" 

df2 <- df1 |>
  mutate(I_group = case_when(
    stringr::str_detect(Inheritance,"^Autosomal d") ~ "AD",
    stringr::str_detect(Inheritance,"^Autosomal r") ~ "AR",
    stringr::str_detect(Inheritance,"^X-linked") ~ "X",
    TRUE ~ Inheritance
  )) |>
#> # A tibble: 8 × 3
#>   Inheritance                             Important_Letter I_group    
#>   <chr>                                   <chr>            <chr>      
#> 1 Autosomal dominant                      a                AD         
#> 2 Autosomal dominant, digenic             b                AD         
#> 3 Autosomal dominant, autosomal recessive c                AD         
#> 4 Autosomal recessive                     d                AR         
#> 5 Autosomal recessive, digenic            z                AR         
#> 6 X-linked autosomal                      y                X          
#> 7 X-linked recessive                      x                X          
#> 8 Garden Pond                             w                Garden Pond
Created on 2022-12-12 with reprex v2.0.2
1 Like

Perfect! hadn't thought of str_detect.


This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.