简体   繁体   English

如何使用 purrr::map family 将函数直接应用于数据框列表,而不是创建新对象

[英]How use purrr::map family to apply function to a list of data frames directly, not create new objects

I want to apply a function to a set of data frames and have those data frames be updated directly, instead of creating new output that I use to overwrite the current data frames.我想将一个函数应用于一组数据帧并直接更新这些数据帧,而不是创建用于覆盖当前数据帧的新输出。

As an example, I have two data frames, df_a and df_b and a function age_category , which adds a column, stating whether the person is a child or an adult, depending on their age.例如,我有两个数据框df_adf_b以及一个函数age_category ,它添加了一个列,说明该人是儿童还是成人,具体取决于他们的年龄。

df_a <- data.frame(Region = c("North", "South"), Age = c(14, 50))
df_b <- data.frame(Staple = c("Rice", "Potato"), Age = c(35, 2))

df_a
>   Region Age
> 1  North  14
> 2  South  50

df_b
>   Staple Age
> 1   Rice  35
> 2 Potato   2

age_category <- function(x){
  x$category <- ifelse(x$Age >= 18, "adult", "child")
  return(x)
}

I create a list of the data frames and apply the function to them.我创建了一个数据框列表并将函数应用于它们。

df_list <- list(df_a, df_b)

library(purrr)
exmpl_1 <- purrr::map(df_list, age_category)
exmpl_1

> [[1]]
>   Region Age category
> 1  North  14    child
> 2  South  50    adult

> [[2]]
>   Staple Age category
> 1   Rice  35    adult
> 2 Potato   2    child

Now I could use exmpl_1[[1]] to overwrite df_a ( df_a <- exmpl_1[[1]] ) and the same for df_b .现在我可以使用exmpl_1[[1]]覆盖df_a ( df_a <- exmpl_1[[1]] ) 和df_b相同。

I am looking for a way to directly have the function overwrite the data frames as they go.我正在寻找一种直接让函数在数据帧运行时覆盖它们的方法。 Since I am not creating any output I would think I would need to change the function and use walk instead of map .由于我没有创建任何输出,我认为我需要更改函数并使用walk而不是map

age_category_alt <- function(x){
  x$category <- ifelse(x$Age >= 18, "adult", "child")
  assign(deparse(substitute(x)), x)
}

walk(df_list, age_category_alt)

But this does not work.但这不起作用。 The data frames do not change and the only outcome is this warning:数据框不会改变,唯一的结果是这个警告:

Warning messages:
1: In assign(deparse(substitute(x)), x) :
  only the first element is used as variable name
2: In assign(deparse(substitute(x)), x) :
  only the first element is used as variable name

I kindly ask for assistance.我恳请帮助。

There are multiple ways to handle this, although I personally prefer to keep data in lists instead of separate dataframes.有多种方法可以解决这个问题,尽管我个人更喜欢将数据保存在列表中而不是单独的数据帧中。

library(purrr)

1) Using named list and age_category function from the OP, we can use map and list2env 1)使用来自OP的命名列表和age_category函数,我们可以使用maplist2env

df_list <- list(df_a = df_a, df_b = df_b)

df_list <- map(df_list, age_category)
list2env(df_list, .GlobalEnv)

df_a
#  Region Age category
#1  North  14    child
#2  South  50    adult

df_b
#  Staple Age category
#1   Rice  35    adult
#2 Potato   2    child

2) Using same named list from above and assign with imap . 2) 使用上面相同的命名列表并使用imap assign

age_category_alt <- function(x, y){
  x$category <- ifelse(x$Age >= 18, "adult", "child")
  assign(y, x, envir = .GlobalEnv)
}

imap(df_list, age_category_alt)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 使用 purrr::map 在数据框列表中的选定列上应用函数 - R use purrr::map to apply a function on a selected columns in a dataframe list 使用purrr :: map将多个参数应用于函数 - Use purrr::map to apply multiple arguments to a function 使用Apply系列功能创建多个新数据框 - Use Apply family function to create multiple new dataframe 如何使用 **purrr** pacakge 中的 `map` 系列命令在数据框中的行之间交换列? - how to use the `map` family command in **purrr** pacakge to swap the columns across rows in data frame? R:如何在不创建列表的情况下使用 purrr 将类似的 mutate() 应用于多个数据帧? - R: How to apply a similar mutate() to multiple data frames with purrr without creating a list? Purrr Map *功能系列中的计数器 - Counter in purrr's map* function family 使用dplyr中的purrr和mutate()将新变量添加到数据帧列表中 - Add new variable to list of data frames with purrr and mutate() from dplyr 如何使用apply-family的合适function列表 - How to use the suitable function of apply-family on a list 使用“应用”功能族处理数据框列表 - Processing the list of data.frames with “apply” family of functions 使用 R 中的 purrr::map() function 将统计测试应用于列表 - Using the purrr::map() function in R to apply a statistical test to a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM