R统计:如何精确地拼写一个单词 [英] R statistics: How to grep a word exactly
问题描述
我想在下面的字符向量中寻找氮,并且想要只返回含有氮的条目并且没有其他条目(例如固氮):
:
I'd like to grep for "nitrogen" in the following character vector and want to get back only the entry which is containing "nitrogen" and nothing of the rest (e.g. nitrogen fixation):
varnames=c("nitrogen", "dissolved organic nitrogen", "nitrogen fixation", "total dissolved nitrogen", "total nitrogen")
我试过这样的事情:
grepl(pattern= "![[:space:]]nitrogen![[:space:]]", varnames)
但这不起作用。我希望有人可以帮助我完成这项任务。
But this doesn't work. I hope somebody can help me with this task.
推荐答案
要获得与氮完全相同的索引,您可以使用
To get the indices that are exactly equal to "nitrogen" you could use
which(varnames == "nitrogen")
根据你想要做什么,你可能甚至不需要'which'作为 varnames ==nitrogen
给出逻辑向量TRUE / FALSE。如果你只是想用氧气来代替所有的氮气的出现,这应该就足够了
Depending on what you want to do you might not even need the 'which' as varnames == "nitrogen"
gives a logical vector of TRUE/FALSE. If you just want to do something like replace all of the occurances of "nitrogen" with "oxygen" this should suffice
varnames[varnames == "nitrogen"] <- "oxygen"
这篇关于R统计:如何精确地拼写一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!