简体   繁体   English

R中特殊字符的grep

[英]grep for special characters in R

I am searching for "1."我正在搜索“1”。 in a matrix in R using grep function.在 R 中使用 grep 函数的矩阵中。 grep("1\\.",VADeaths,value=TRUE). grep("1\\.",VADeaths,value=TRUE)。 However, it is not showing me 41.0 in the result.但是,它没有在结果中显示 41.0。 Why is it so?为什么会这样? 41.0 is one of the values in one of the columns. 41.0 是其中一列中的值之一。

If we convert in to character , this will become more apparent如果我们转换为character ,这将变得更加明显

as.character(VADeaths)
#[1] "11.7" "18.1" "26.9" "41"   "66"   "8.7"  "11.7" "20.3" "30.9" "54.3" 
#[11] "15.4" "24.3" "37"   "54.6" "71.1" "8.4"  "13.6" "19.3" "35.1" "50"  

For 41 , it is just round and there is no .对于41 ,它只是圆的,没有. there那里

If we need to get those elements as well如果我们也需要获取这些元素

grep("1\\.|^[^.]*1$", VADeaths, value = TRUE)
#[1] "11.7" "41"   "11.7" "71.1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM