0

Getting TRUE in str_detect for the pattern which is not present. Checking for dot(.) as the pattern in the population column of data frame

#data frame

tab<- data.frame(
  state=c("Alabama","Alaska","Arizona","Arkansas","California"),
  population=c("4,853,875"," 737,709","6,817,565","2,977,853","38,993,940"),
  total=c("348","59","309","181","1,861"),
  murder_rate= c(7.2,8.0,4.5,6.1,4.8)

)

library(tidyverse)
str_detect(tab$population,".") #checking for pattern = .
#output: TRUE TRUE TRUE TRUE TRUE

checked also for pattern = ',' it's giving TRUE again.

str_detect(tab$population,",") 
#output: TRUE TRUE TRUE TRUE TRUE

Not sure what is wrong?

Teena Joy
  • 1
  • 3
  • 1
    you need to escape the "." character. `str_detect(df$population, '\\.')` – Ronak Shah May 03 '20 at 09:53
  • ok thanks..but...str_detect(tab, ",") is working without escape characters.. why so? – Teena Joy May 03 '20 at 09:56
  • because "." has a special meaning in regex. So if you want to match a literal dot you need to escape it. Other such symbols with special meaning are "|", "$" etc. – Ronak Shah May 03 '20 at 10:04

0 Answers0