0

For example, if I want to keep only those rows of the data mtcars where the variable qsec contains this decimal .50, following the solutions given here, I use:

mtcars_stringed<-mtcars%>%filter(str_detect(qsec, ".50"))
mtcars_stringed<-mtcars[mtcars$qsec %like% ".50", ]
mtcars_stringed <- mtcars[grep(".50", mtcars$qsec), ]

View(mtcars_stringed)

Surprisingly, all these strategies fail, by returning null, while in fact mtcars$qsec has values containing .50 such as 14.50, 15.50,

Any alternative solution, or is there something I am missing? Thanks in advance.

Krantz
  • 1,424
  • 1
  • 12
  • 31

2 Answers2

3

When you treat a numeric as a string, it is converted as.character(mtcars$qsec). If you look at that, you'll see that in the conversion, trailing 0s are dropped, so we get, e.g., "14.5", "15.5".

It will work if you use the regex pattern "\\.5$", \\ to make the . a ., not just "any character", and $ to match the end of the string.

mtcars %>% filter(str_detect(qsec, "\\.5$"))
#    mpg cyl disp  hp drat   wt qsec vs am gear carb
# 1 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
# 2 19.7   6  145 175 3.62 2.77 15.5  0  1    5    6

However, in general, treating decimals as strings can be risky. A better approach might to get rid of the integer with %% 1 and then test for nearness to 0.5 within some tolerance, this will avoid precision issues.

mtcars %>% filter(abs(qsec %% 1 - 0.5) < 1e-10)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
1

You are probably looking for:

mtcars %>%
 filter(qsec %% 0.50 == 0 & qsec %% 1 != 0)

   mpg cyl disp  hp drat   wt qsec vs am gear carb
1 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
2 19.7   6  145 175 3.62 2.77 15.5  0  1    5    6
tmfmnk
  • 38,881
  • 4
  • 47
  • 67