使用purrr创建新变量(该怎么做?)

编程入门 行业动态 更新时间:2024-10-27 20:32:09
本文介绍了使用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创建新变量(该怎么做?)

本文发布于:2023-10-18 02:13:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:怎么做   变量   purrr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!