When I use dplyr::filter with a sequence and %in% it randomly leaves out rows that it shouldn't. Is there a better way to filter the data so that I reliably get a df that includes every value of q from 0.01 to 1 by steps of 0.01?
Here is a snippet of my data to create df
df <- structure(list(q = c(0.0495185253755619, 0.05, 0.0532000452215362,
0.0569525370086692, 0.06, 0.0646716714872386, 0.07, 0.0767903072707,
0.08, 0.0809750285664481, 0.09, 0.0939688126826123, 0.1, 0.103000546236258,
0.11, 0.117107570056396), r_timestamp = structure(c(1403667900,
NA, 1403668800, 1403669700, NA, 1403670600, NA, 1403671500, NA,
1403672400, NA, 1403673300, NA, 1403674200, NA, 1403675100), class = c("POSIXct",
"POSIXt"), tzone = "Etc/GMT-4"), NO3_rise = c(0.0482379790550339,
NA, 0.0482408804822149, 0.0496608873041167, NA, 0.0510808941260188,
NA, 0.053096735586062, NA, 0.0551125770461051, NA, 0.0559331273472383,
NA, 0.0567536776483717, NA, 0.0531344453067981)), row.names = c(NA,
-16L), class = "data.frame")
Here is the code. The resulting df2
should have 7 rows and a q
value for 0.05 to 0.11 by steps of 0.01. The code currently returns df2
with only 4 lines including q
values of 0.05, 0.08, 0.09, 0.11.
# Packages
library("tidyverse")
library("lubridate")
library("zoo")
# Code chunk
df2 <- df %>%
# Interpolate missing solute values
mutate_at(vars(c(NO3_rise)),
funs(na.approx(., x = q, xout = q, na.rm = FALSE))) %>%
# Only keep rows where q value matches sequence below
filter(q %in% seq(0.01, 1, by = 0.01))