I am trying to automatically generate several new variables based on existing variables. I would like to get the range of values for each of "a", "b" and "c" using their respective min and max variables. The data I am simulating are from satellite sensors aggregated to zonal statistics which would mean that each row is a polygon feature.
Here is a toy data frame to use:
dat <- data.frame(a.min = runif(100, 0, 100),
b.min = runif(100, 0, 10),
c.min = runif(100, 0, 0.5),
a.max = runif(100, 100, 200),
b.max = runif(100, 10, 20),
c.max = runif(100, 0.5, 1))
Here is the manual way of performing this action:
dat$a.range <- dat$a.max - dat$a.min
dat$b.range <- dat$b.max - dat$b.min
dat$c.range <- dat$c.max - dat$c.min
head(dat)
How can accomplish this in an automated fashion with dplyr? I know that I will have NA values in my data.
So far I have:
dat %>% select(dat, matches("min|max"))
I tried to define a range function:
rng <- function(x,y){y - x})
I am not sure where to go after select. I think I need to use "mutate" or "across"?
Cheers and thanks!