Something like this perhaps:
library(tidyverse)
d <- seq(0, 100, 0.5)
Fe <- runif(201, min = 0, max = 1000)
Ca <- runif(201, min = 0, max = 1000)
Zr <- runif(201, min = 0, max = 1000)
Ti <- runif(201, min = 0, max = 1000)
Al <- runif(201, min = 0, max = 1000)
example <- data.frame(d, Fe, Ca, Zr, Ti, Al)
Ratio_Elements <- c("Fe", "Ti", "Zr", "d") #this subset of the
Example_Ratio <- example %>%
mutate_at(vars(-Zr), funs(. / Zr)) %>%
select(Ratio_Elements)
I know you said you'd like to see a mutate_all
solution, but I guess you don't want to divide Zr
by itself?
In this case mutate_at
is more helpful, otherwise you can do mutate_all(funs(. / Zr))
.
If you want to keep the mentioned vector:
Detrital_Divisor <- as.symbol("Zr")
Example_Ratio <- example %>%
mutate_at(vars(- !! Detrital_Divisor), funs(. / !! Detrital_Divisor)) %>%
select(Ratio_Elements)
UPDATE (dplyr 0.8.0)
Now that funs
is deprecated as of dplyr 0.8.0
, you can just use ~
, e.g.:
Detrital_Divisor <- as.symbol("Zr")
Example_Ratio <- example %>%
mutate_at(vars(- !! Detrital_Divisor), ~ . / !! Detrital_Divisor) %>%
select(Ratio_Elements)
On the other hand there's also list
- useful for mutating in multiple ways or naming the output, e.g.:
Example_Ratio <- example %>%
mutate_at(vars(- !! Detrital_Divisor), list(appended_name = ~ . / !! Detrital_Divisor))
UPDATE (dplyr 1.0.0)
As of dplyr 1.0.0
, you would probably want to use across
:
Example_Ratio <- example %>%
mutate(across(-Detrital_Divisor, ~ . / !! Detrital_Divisor)) %>%
select(all_of(Ratio_Elements))