【问题标题】:how to remove a very special character in a df如何删除df中非常特殊的字符
【发布时间】:2021-10-22 07:23:59
【问题描述】:

我有一个 df,其中有一个特殊字符。我想删除它,但不确定如何删除。我试过[:graph:][:print:]<U+00AE>。但没有任何效果。我该怎么办?是否有办法一次性去除数据集中的类似问题,比如®

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))

【问题讨论】:

  • 也许dplyr::mutate(df, df = stringr::str_remove(df, "\\x{00AE}")) 会起作用?
  • 它可能需要一些替换然后转换为打印,即library(stringi);stri_unescape_unicode(str_replace_all(df$df, "&lt;U\\+([^&gt;]+)\\&gt;", "\\\\u\\1"));[1] "Cabozantinib" [2] "XmAb®20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)"...
  • 如果我转换为®,那我应该如何删除它?如果我使用gsub("[[:print:]]", "",df$df ),那么它将全部删除。事实上我的原始数据有®。但是当我把它读成 R 时,它把它变成了这个奇怪的租船者。有什么建议吗?
  • @Stataq 将其与符号保持原样而不是删除它不是更好吗。
  • 我在构建 wb 并将数据再次输出到 excel 时遇到了问题。我测试了很多次,发现这是停止文件的记录。无论如何要解决它?

标签: r regex


【解决方案1】:

如果打算删除这些字符,请使用模式匹配&lt;U,后跟+(元字符 - 转义\\),后跟一个或多个不是&gt;的字符( [^&gt;]+) 和str_remove_all 中的&gt; 删除该模式子字符串的所有出现

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"              

如果我们还想打印那些 unicode 字符

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"            

【讨论】:

  • 这里只是对[^&gt;] 字符类的最简短评论,供那些被“这是他们给我的文件......”所困扰的人使用。
  • 当我尝试使用我的原始数据时,它现在看起来像 "XmAb&lt;U+FFFD&gt;20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)" 并且不会改变。 &lt;U+xxx~ &gt; 会不会每次阅读都会改变?
  • @Stataq 您是否将输出分配回列,即df$df &lt;- ..
  • @Stataq 它确实改变了.e。 str_remove_all("XmAb&lt;U+FFFD&gt;20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)", "&lt;U\\+[^&gt;]+\\&gt;")# [1] "XmAb20717 (Duet-2 study - a humanized bispecific monoclonal antibody that binds PD1 and CTLA4)"
  • 您的代码适用于 df。 df 是我的原始数据集的一个子集。
猜你喜欢
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 2022-08-03
相关资源
最近更新 更多