I'm trying to use mutate/str_replace to generate "Phenotype' from "Class" by removing parenthesis (including contents) but need some help with the Regex? I would also like to then reorder the text within "Phenotype" strings such that text is shown in order PanCK>PD-L1>CD8>FoxP3>PD-1>CD68. Apologies for the non-standard dataset! Many thanks!
test<- data.frame(Class = c("FoxP3 (Opal 570): PanCK (Opal 690): PD-1 (Opal 620): CD68 (Opal 780)"
, "CD8 (Opal 480): PanCK (Opal 690): CD68 (Opal 780): PD-L1 (Opal 520)", "PanCK (Opal 690): CD68 (Opal 780)",
"FoxP3 (Opal 570): PanCK (Opal 690)"))
The bit I'm having trouble with
test.output<- test %>% mutate(Phenotype = str_replace(Class, "\\([^()]{0,}\\)", ""))
desired output:
test.output <- data.frame(Class = c("FoxP3 (Opal 570): PanCK (Opal 690): PD-1 (Opal 620): CD68 (Opal 780)"
, "CD8 (Opal 480): PanCK (Opal 690): CD68 (Opal 780): PD-L1 (Opal 520)",
"PanCK (Opal 690): CD68 (Opal 780)", "FoxP3 (Opal 570): PanCK (Opal 690)"),
Phenotype = c("FoxP3:PanCK:PD-1:CD68", "CD8:PanCK:CD68:PD-L1",
"PanCK:CD68", "CD8:PanCK:CD68:PD-L1"))
to then be reordered such that PanCK>PD-L1>CD8>FoxP3>PD-1>CD68
ordered.output<- data.frame(Class = c("FoxP3 (Opal 570): PanCK (Opal 690): PD-1 (Opal 620): CD68 (Opal 780)"
, "CD8 (Opal 480): PanCK (Opal 690): CD68 (Opal 780): PD-L1 (Opal 520)",
"PanCK (Opal 690): CD68 (Opal 780)", "FoxP3 (Opal 570): PanCK (Opal 690)"),
Phenotype = c("FoxP3:PanCK:PD-1:CD68", "CD8:PanCK:CD68:PD-L1",
"PanCK:CD68", "CD8:PanCK:CD68:PD-L1"),
Phenotype_Ordered = c("PanCK:FoxP3:PD-1:CD68", "PanCK:PD-L1:CD8:CD68",
"PanCK:CD68","PanCk:PD-L1:CD8:CD68"))