How to refer to "e-06" in code?

I am trying to do something like this:

data %>%
  filter(col1 == 1.03e-06)

Is there a way to reference e-06 without having to type out the entire number?

I think you've already got it (provided the number is exactly that):

library(tidyverse)

df <- data.frame(col1 = c(1.03e-06, 0.00000103))

df %>% 
  filter(col1 == 1.03e-06) #should  return both rows

You need to be careful when dealing with floating point numbers though, so you should use a function like all.equal() In such cases.

1 Like

Thank you for your response. How would it look like if I do have floating point numbers? For example:

data %>%
  filter(which(round(col1) == 7.728055e-06))

Don't == with floats.

You may fall into the floating point trap.

Also, calling round on your column is going to give you a vector of integers. Those shouldn't ever be == to 7.7e-6

Use near

@martin.R meant you should use the near function when comparing floating point numbers using dplyr

df %>% filter(near(col1, 7.72e-06))
2 Likes