gpt4 book ai didi

删除 R 中包含冒号的字符串

转载 作者:行者123 更新时间:2023-12-01 01:46:13 24 4
gpt4 key购买 nike

这是我的数据集的示例摘录。它看起来像这样:

Description;ID;Date
wa119:d Here comes the first row;id_112;2018/03/02
ax21:3 Here comes the second row;id_115;2018/03/02
bC230:13 Here comes the third row;id_234;2018/03/02

我想删除那些包含冒号的单词。在这种情况下,这将是 wa119:d、ax21:3 和 bC230:13,因此我的新数据集应如下所示:
Description;ID;Date
Here comes the first row;id_112;2018/03/02
Here comes the second row;id_115;2018/03/02
Here comes the third row;id_234;2018/03/02

不幸的是,我无法使用 gsub 找到正则表达式/解决方案?任何人都可以帮忙吗?

最佳答案

这是一种方法:

## reading in yor data
dat <- read.table(text ='
Description;ID;Date
wa119:d Here comes the first row;id_112;2018/03/02
ax21:3 Here comes the second row;id_115;2018/03/02
bC230:13 Here comes the third row;id:234;2018/03/02
', sep = ';', header = TRUE, stringsAsFactors = FALSE)

## \\w+ = one or more word characters
gsub('\\w+:\\w+\\s+', '', dat$Description)

## [1] "Here comes the first row"
## [2] "Here comes the second row"
## [3] "Here comes the third row"

更多信息 \\w[A-Za-z0-9_] 相同的速记字符类: https://www.regular-expressions.info/shorthand.html

关于删除 R 中包含冒号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com