使用purrr创建新变量(该怎么做?) [英] Creating new variables with purrr (how does one go about that?)

查看:75
本文介绍了使用purrr创建新变量(该怎么做?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据集,有一堆列,我想根据前缀或后缀在其上运行相同的函数,以创建一个新变量.

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创建新变量(该怎么做?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆