Logical operator not seeming to work in mutate()

GOAL

I am trying to edit some data I have and though it seems super straight forward, the condition in my logical statements seem to always be defaulting to false. This should be really simple but I cant seem to get the function to recognize which rows have 200 for the value in sampling_rate and then change the value of other corresponding columns. Any and all help is appreciated.

Data

https://gitlab.com/Bryanrt-geophys/sac2eqtransformr/-/raw/master/sample_data/https://gitlab.com/Bryanrt-geophys/sac2eqtransformr/-/raw/master/sample_data/pre_hdf5_unnested_filtered.rds 

I still haven't heard of a good way to share RDS files so I just plug this into my browser and it downloads the file.

REPREX

pre_hdf5_filtered <- pre_hdf5_unnested_filtered %>%
  nest(sac_EV = sac_EV) %>%
  mutate(
    sac_p_index = if_else(sampling_rate == 200, sac_p_index/2, sac_p_index),
    sac_s_index = if_else(sampling_rate == 200, sac_s_index/2, sac_s_index),
    sac_coda_index = if_else(sampling_rate == 200, sac_coda_index/2, sac_coda_index),
    sac_index_end = if_else(sampling_rate == 200, sac_index_end/2, sac_index_end),
    sampling_rate = if_else(sampling_rate == 200, 100, 100)
  )

No idea why this always defaults to false.

Your data is not exactly 100 and 200,

 ph$sampling_rate %>% table

100.000002235174 200.000004470348 
         1608268           486081 

a dplyr approach would be to avoid == and use dplyr::near with an appropriate tolerance, like

near(200 ,
200.000004470348 , 
0.00001)
# TRUE

Or you could look at a) rounding your data to an approriate precision,
b) if the data should even be integer rather than a double float, cast it to integer type.

Thank you so much! Im not sure how the data ended up having decimal values as I read them in as characters and then did an as.numeric. This is good to be aware of in the future.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.