使用 Purrr 映射多个数据帧并将这些修改后的数据帧作为输出 [英] Map with Purrr multiple dataframes and have those modified dataframes as the output
问题描述
我有一个关于 Purrr 包中的地图功能的问题.
I've got a question with the map function from the Purrr package.
- 我可以使用 map 成功地将数据框列表传递给函数
- 输出仍然是一个列表,这是我的问题;我需要将修改后的数据框作为 R 对象
以 mtcars 数据集为例:
As an example with the mtcars dataset:
#I create a second df
mtcars2 <- mtcars
#change one variable just to distinguish them
mtcars2$mpg <- mtcars2$mpg / 2
#create the list
dflist <- list(mtcars,mtcars2)
#then, a simple function example
my_fun <- function(x)
{x <- x %>%
summarise(`sum of mpg` = sum(mpg),
`sum of cyl` = sum(cyl)
)
}
#then, using map, this works and prints the desired results
list_results <- map(dflist,my_fun)
但是,我需要将修改后的 mtcars 和 mtcars2 保存为 r 对象(数据帧).
But, I would need to have the modified mtcars and mtcars2 saved as r objects (dataframes).
- 我应该为我的函数添加某种保存"选项吗?
- 我应该使用 map_df 还是 dmap ?(我的试验没有成功)
提前,非常感谢你们!
推荐答案
这是一个尝试:
library(purrr)
library(tidyverse)
mtcars2 <- mtcars
mtcars2$mpg <- mtcars2$mpg / 2
dflist <- list(mtcars,mtcars2)
要保存对象,需要为它们指定特定名称,并使用:
To save the objects one would need to give them specific names, and use:
assign("name", object, envir = .GlobalEnv)
这是实现这一目标的一种方法:
here is one way to achieve that:
my_fun <- function(x, list) {
listi <- list[[x]]
assign(paste0("object_from_function_", x), dflist[[x]], envir = .GlobalEnv)
x <- listi %>%
summarise(`sum of mpg` = sum(mpg),
`sum of cyl` = sum(cyl)
)
return(x)
}
my_fun
有两个参数 - seq_along(list)
生成特定名称和要处理的 list
my_fun
has two arguments - seq_along(list)
to generate specific names and the list
that is to be processed
这会保存两个对象 object_from_function_1
和 object_from_function_2
:
this saves two objects object_from_function_1
and object_from_function_2
:
list_results <- map(seq_along(dflist), my_fun, dflist)
另一种方法是在地图函数之外使用 list2env
作为 akrun 建议
another approach would be to use list2env
outside of the map function as akrun suggested
dflist <- list(mtcars,mtcars2)
names(dflist) <- c("mtcars","mtcars2")
list2env(dflist, envir = .GlobalEnv) #this will create two objects `mtcars` and `mtcars2`
并在创建对象后运行 map
,就像您已经完成的那样.
and run map
after you have created the objects as you have already done.
这篇关于使用 Purrr 映射多个数据帧并将这些修改后的数据帧作为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!