how to remove a very special character in a df

Viewed 53

I have a df which have a special character in it. I would like to remove it but not sure how. I have tried [:graph:], [:print:] and <U+00AE>. but nothing works. what should I do? Is it a way to remove similar problem in a dataset at once, such as ®?

enter image description here

df<-structure(list(df = structure(c(1L, 4L, 2L, 3L), .Label = c("Cabozantinib", 
"Left nephrectomy", "Left Superficial Inguinal Lymph Node Dissection", 
"XmAb<U+00AE>20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L))
1 Answers

If the intention is to remove those characters, use the pattern to match the <U followed by the + (metacharacter - escaped \\), followed by one or more characters that are not a > ([^>]+) and the > in str_remove_all to remove all occurence of that pattern substring

library(stringr)
df$df <- str_remove_all(df$df, "<U\\+[^>]+\\>")
df$df
[1] "Cabozantinib"                                                                                  
[2] "XmAb20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)"
[3] "Left nephrectomy"                                                                              
[4] "Left Superficial Inguinal Lymph Node Dissection"              

If we still want to print those unicode characters

library(stringi)
stri_unescape_unicode(str_replace_all(df$df, "<U\\+([^>]+)\\>", "\\\\u\\1"))
[1] "Cabozantinib"                                                                                   
[2] "XmAb®20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)"
[3] "Left nephrectomy"                                                                               
[4] "Left Superficial Inguinal Lymph Node Dissection"            
Related