简体   繁体   English

如何去除R中的无用字符?

[英]How to remove useless characters in R?

I have a dataset like below, how can I remove the '#number'?我有一个如下所示的数据集,如何删除“#number”?

df>
terms                             year
5;#Remote Production;#10;         2021
53;#=Product-Category:Routing     2021
30;#HDR;#5;#Remote Production     2020
...

I need it to be like this:我需要它是这样的:

df>
terms                          year
#Remote Production             2021
#Product-Category:Routing      2021
#HDR;#Remote Production     2020
...

The number at the beginning without the # also needs to be removed开头没有#的数字也需要去掉

An option with str_remove str_remove的一个选项

library(stringr)
library(dplyr)
df %>%
   mutate(terms = str_c('#', str_remove_all(terms, "^\\d+;#\\=?|#\\d+;")))

-output -输出

#                     terms year
#1       #Remote Production; 2021
#2 #Product-Category:Routing 2021
#3   #HDR;#Remote Production 2020

data数据

df <- structure(list(terms = c("5;#Remote Production;#10;", "53;#=Product-Category:Routing", 
"30;#HDR;#5;#Remote Production"), year = c(2021L, 2021L, 2020L
)), class = "data.frame", row.names = c(NA, -3L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM