How to grep a word exactly

前端 未结 3 1391
礼貌的吻别
礼貌的吻别 2020-12-03 22:11

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. nitro

3条回答
  •  不思量自难忘°
    2020-12-03 22:55

    To get the indices that are exactly equal to "nitrogen" you could use

    which(varnames == "nitrogen")
    

    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"
    

提交回复
热议问题