3

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!

1 Answer 1

1

Here is an option to split the dataset into a list of data.frame based on the column name pattern, then reduce it by dividing elementwise on each pair of columns in each of the dataset and bind with the original dataset

library(tidyverse)
df %>% 
   split.default(sub("\\d+", "", names(.))) %>% 
   map_df(reduce, `/`) %>% 
   rename_all(~paste0(., 3)) %>% 
   bind_cols(df, .)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.