I'm trying to figure out how to create multiple new variables that are calculated using variables currently in my dataset
Here's some example data
library(tidyverse)
df <- data.frame(
a1 = rnorm(100),
a2 = rnorm(100),
b1 = rnorm(100),
b2 = rnorm(100),
c1 = rnorm(100),
c2 = rnorm(100)
)
Essentially, I want to create a new variable for each a, b, c pair that divides a1 by a2, b1 by b2, etc. For example:
df <- df %>%
mutate(a3 = a1/a2)
The variables in my dataset don't follow these naming conventions, so I feel like I need to assign names to vectors:
numerators <- c('a1', 'b1', 'c1')
denominators <- c('a2', 'b2', 'c2')
And then creating a new variable would follow the basic convention
mutate(newvars = numerators/denominators)
But I'm stuck as to how to actually do this. Any help would be much appreciated -- thanks!