Ok, all.equal()
turned out not to be a straightforward solution because is also not vectorized, luckily, dplyr
has a vectorized version, near()
.
library(dplyr)
# Sample data on a copy/paste friendly format
site <- data.frame(
Ozone = c(41, 36, 12, 18, NA, 28, 23, 19, 8, NA, 7, 16),
Solar.R = c(190, 118, 149, 313, NA, NA, 299, 99, 19, 194, NA, 256),
Wind = c(7.4, 8, 12.6, 11.5, 14.3, 14.9, 8.6, 13.8, 20.1, 8.6, 6.9, 9.7),
Temp = c(67, 72, 74, 62, 56, 66, 65, 59, 61, 69, 74, 69),
Month = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5),
Day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
)
# Relevant code
site %>%
mutate(fxn_wnd = case_when(
Wind > 9.7 ~ exp((Wind+log(1+Wind^3)))/Wind^2,
near(Wind, 9.7) ~ 10,
TRUE ~ (2+Wind^3)/Wind
))
#> Ozone Solar.R Wind Temp Month Day fxn_wnd
#> 1 41 190 7.4 67 5 1 5.503027e+01
#> 2 36 118 8.0 72 5 2 6.425000e+01
#> 3 12 149 12.6 74 5 3 3.738506e+06
#> 4 18 313 11.5 62 5 4 1.135978e+06
#> 5 NA NA 14.3 56 5 5 2.322179e+07
#> 6 28 NA 14.9 66 5 6 4.408647e+07
#> 7 23 299 8.6 65 5 7 7.419256e+01
#> 8 19 99 13.8 59 5 8 1.359278e+07
#> 9 8 19 20.1 61 5 9 1.077876e+10
#> 10 NA 194 8.6 69 5 10 7.419256e+01
#> 11 7 NA 6.9 74 5 11 4.789986e+01
#> 12 16 256 9.7 69 5 12 1.000000e+01
Created on 2022-04-29 by the reprex package (v2.0.1)
Even though I would strongly recommend you to work a little bit on your basic R coding skills, you can't just patch random code together and expect it to work, at the very least you need to read the documentation for the functions you intend to use.