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

蹲街弑〆低调 提交于 2021-01-28 18:19:42

问题


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))


回答1:


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, .)


来源:https://stackoverflow.com/questions/52607511/creating-new-variables-with-purrr-how-does-one-go-about-that

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!