我有一个很大的数据集,有一堆列,我想根据前缀或后缀在其上运行相同的函数,以创建一个新变量.
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)))是否有可能符合以下要求:newframe<-dataframe%>%map(c("x","y"),mutate(paste0(.x,"_ ratio)= paste0(.x," _ 1/",. x," _ 2))
Is it possible with something along the lines of: newframe <- dataframe %>% map(c("x","y"), mutate( paste0(.x,"_ratio)=paste0(.x,"_1/",.x,"_2))
推荐答案如果我们要使用 map ,则一种选择是通过列名分割数据集并用 reduce
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, .)更多推荐
使用purrr创建新变量(该怎么做?)
发布评论