当 R 中以小写字母开头时,从数据框单元格中删除第一个单词

发布于 2025-01-16 13:02:34 字数 870 浏览 2 评论 0原文

我想要清理 R 中包含细菌种类的分类表,并且想要删除所有以小写字母开头的单元格中的值。

我有一个来自分类学 df 的专栏:

物种
Tuwongella immobilis
Woesebacteria
未知海洋
细菌 Ellin506

我想要:

物种
Tuwongella immobilis
Woesebacteria
unwanted <- "^[:upper:]+[:lower:]+"
tax.clean$Species <- str_replace_all(tax.clean$Species, unwanted, "")

但它似乎不起作用,并且与所需的物种不匹配。

I want to clean up a taxonomy table with bacterial species in R and I want to delete values from all cells that start with the small letter.

I have a column from taxonomy df:

Species
Tuwongella immobilis
Woesebacteria
unidentified marine
bacterium Ellin506

And I want:

Species
Tuwongella immobilis
Woesebacteria
unwanted <- "^[:upper:]+[:lower:]+"
tax.clean$Species <- str_replace_all(tax.clean$Species, unwanted, "")

but it doesn't seem to work and does not match desired species.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2025-01-23 13:02:35

如果您正在使用数据帧,我建议使用 dplyr::filter 来清理数据帧。

grepl() 返回逻辑值,!grepl(^[[:lower:]]) 查找任何不以小写字母开头的内容 (^ 表示字符串的开头)。

library(dplyr)

df %>% filter(!grepl("^[[:lower:]]", Species))

               Species
1 Tuwongella immobilis
2        Woesebacteria

If you are working with dataframe, I suggest using dplyr::filter to clean up the dataframe.

grepl() returns logical values, !grepl(^[[:lower:]]) looks for anything that does not start with a lower case letter (^ indicate the beginning of a string).

library(dplyr)

df %>% filter(!grepl("^[[:lower:]]", Species))

               Species
1 Tuwongella immobilis
2        Woesebacteria
风月客 2025-01-23 13:02:35

我们可以做

grep('^[A-Z]', df$Species, value = T)
[1] "Tuwongella immobilis" "Woesebacteria" 

We can do

grep('^[A-Z]', df$Species, value = T)
[1] "Tuwongella immobilis" "Woesebacteria" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文