r - 在 R 中的冒号之前删除单词时出错
问题描述
我有以下数据框
head(df)
index song year artist genre lyrics
2 Till i am gone 2010 Eminem Rap Chorus:It's too much, it's too tough
我已经完成了其他数据清理,例如使用 gsub 将所有内容转换为小写并删除括号之间的单词,但是,没有找到仅删除单词和它之后的冒号的语法,例如在我的行中,我想删除“合唱:”
在语法之后它应该是
lyrics
It's too much, it's too tough
以下代码将删除我不想要的冒号之前的所有内容,因为该冒号可以在单元格中的任何位置
gsub(".*:","",foo)
解决方案
您可以指定仅删除冒号之前的单词。我扩展了您的测试集以显示它有效。
foo = c("Chorus:It's too much, it's too tough ",
"ABC Chorus:It's too much, it's too tough ")
gsub("\\w+:", "", foo)
[1] "It's too much, it's too tough " "ABC It's too much, it's too tough "
推荐阅读
- c++17 - 对输入/输出变量使用“std::shared_ptr”是否合理?
- typescript - Typescript 在抽象类的子类中获取正确的返回类型
- python - 标准化的html出现异常入口如何处理
- google-tag-manager - 像素跟踪是否需要 js 代码才能在 Google 跟踪代码管理器上运行?
- process - 如何制作场景:该进程无法访问该文件,因为它正在被另一个进程使用
- service - 服务到服务通信的授权码授予类型
- c++ - 如何在Boost几何中使用多个线串创建盒子?
- python - 增加多个直方图的大小
- azure-data-explorer - 如何在 Azure 数据资源管理器/Kusto 中根据子字符串匹配连接两个表?
- php - 多线程问题写入php文件