首页 > 解决方案 > 试图删除第一次出现的字符 |

问题描述

我正在尝试删除|数据中的第一个。当我运行以下命令时:

yy <- y %>% 
  mutate_all(funs(sub("|", "", .)))

什么都没发生。

我在其他列中还有其他出现,|所以我不想删除所有这些。我能够|使用以下内容删除最后一次出现的mutate_all(funs(gsub("(.*)\\|(.*)", "\\1\\2", .)))- 现在我想做同样的事情,但第一次出现。

数据:

y <- structure(list(grp = c("2902", "9738", "9143", "3990", "10488", 
"9581", "10905", "9859", "8787", "8626"), HD = c("| CarrolsRestr 4Q EPS 16c Vs EPS 22c       ", 
"| Press Release: Fitch Affirms Aeroports de Paris SA at 'A+'; Outlook Stable    ", 
"| MSC Reports Fiscal 2017 First Quarter Results    ", "| Goodrich Announces Third Quarter 2009 Net Income per Diluted Share of $1.14, Adjusts Outlook for Full Year 2009, Provides Outlook for 2010    ", 
"| *Vera Bradley 1Q Loss/Shr 4c >VRA    ", "| *Fitch Affirms One Re's IFS at 'BBB-'; Outlook Stable    ", 
"| Johnson Controls 2014 first quarter earnings increase 31 percent on higher revenues and improved profitability; affirms guidance for fiscal 2014    ", 
"| eBay Inc. Reports Third Quarter 2017 Results    ", "| S&P Rating Shift a Sober Reminder for Australia -- Market Talk    ", 
"| Jeff Immelt: GE 'Underowned' by Big Investors    "), WC = c("| 191 words    ", 
"| 2,493 words    ", "| 2,068 words    ", "| 6,275 words    ", 
"| 3,912 words    ", "| 1,907 words    ", "| 2,452 words    ", 
"| 5,227 words    ", "| 1,594 words    ", "| 920 words    ")), class = "data.frame", row.names = c(NA, 
-10L))

标签: r

解决方案


这对我有用:

y$HD <- sub("|", "", y$HD, fixed = TRUE)
y$WC <- sub("|", "", y$WC, fixed = TRUE)

推荐阅读