Will.I.am
Will.I.am

Reputation: 25

Creating new variables with purrr (how does one go about that?)

I have a large data set, with a bunch of columns that I want to run the same function on, based on either prefix or suffix, to create a new variable.

What I would like to be able to do is provide a list to map, and create new variables.


dataframe <- data_frame(x_1 = c(1,2,3,4,5,6),
                        x_2 = c(1,1,1,2,2,2),
                        y_1 = c(200,400,120,300,100,100),
                        y_2 = c(250,500,150,240,140,400))

newframe <- dataframe %>% mutate(x_ratio = x_1/x_2,
                                 y_ratio = y_1/y_2)

In the past, i have written code in a string something like

code <- "df <- df %>% mutate(#_ratio = #_1/#_2)" %>% str_replace_all("#",c("x","y"))
eval(parse(text=code))) 

Is it possible with something along the lines of: newframe <- dataframe %>% map(c("x","y"), mutate( paste0(.x,"_ratio)=paste0(.x,"_1/",.x,"_2))

Upvotes: 1

Views: 613

Answers (1)

akrun
akrun

Reputation: 886948

If we want to use map, then one option is to split the dataset by the column names and divide with reduce

library(tidyverse)
split.default(dataframe, sub("_\\d+", "", names(dataframe))) %>%
     map_df(., reduce, `/`) %>% 
       rename_all(~ paste0(.x, "_ratio")) %>%
       bind_cols(dataframe, .)

Upvotes: 1

Related Questions