简体   繁体   English

将data.frame列名传递给使用purrr :: map的函数

[英]Pass a data.frame column name to a function that uses purrr::map

I'm working with nested dataframes and want to pass the name of the top level dataframe, and the name of a column containing lower level dataframes, to a function that uses purrr::map to iterate over the lower level data frames. 我正在使用嵌套数据框,并希望将顶层数据框的名称以及包含较低层数据框的列的名称传递给使用purrr :: map遍历较低层数据框的函数。

Here's a toy example. 这是一个玩具示例。

library(dplyr)
library(purrr)
library(tibble)
library(tidyr)

df1 <- tibble(x = c("a","b","c", "a","b","c"), y = 1:6)
df1 <- df1 %>%
  group_by(x) %>%
  nest()

testfunc1 <- function(df) {
  df <- df %>%
    mutate(out = map(data, min))
  tibble(min1 = df$out)
}

testfunc2 <- function(df, col_name) {
  df <- df %>%
    mutate(out = map(col_name, min))
  tibble(min2 = df$out)
}

df1 <- bind_cols(df1, testfunc1(df1))
df1 <- bind_cols(df1, testfunc2(df1, "data"))

df1$min1
df1$min2

testfunc1 behaves as expected, in this case giving the minimum of each data column in a new column. testfunc1的行为符合预期,在这种情况下,将在新列中提供每个数据列的最小值。 In testfunc2, where I've tried to pass the column name, a string reading "data" is passed to the new column. 在testfunc2中,我尝试传递列名,将读取“数据”的字符串传递给新列。 I think I understand from the thread here ( Pass a data.frame column name to a function ) why this doesn't behave as I want, but I haven't been able to figure out how to make it work in this case. 我想我从这里的线程中了解了( 将data.frame列名传递给函数 )为什么这种行为不符合我的期望,但是我无法弄清楚在这种情况下如何使其工作。 Any suggestions would be great. 任何建议都很好。

This should work for you, it uses the tidy eval framework. 这应该为您工作,它使用整洁的评估框架。 This assumes col_name is a string. 假设col_name是一个字符串。

testfunc2 <- function(df, col_name) {
     df <- df %>%
          mutate(out = map(!! rlang::sym(col_name), min))
    tibble(min2 = df$out)

}

EDIT: 编辑:

If you'd rather pass a bare column name to the function, instead of a string, use enquo instead of sym . 如果您希望将裸列名称(而不是字符串)传递给函数,请使用enquo而不是sym

testfunc2 <- function(df, col_name) {
     col_quo = enquo(col_name)
     df <- df %>%
          mutate(out = map(!! col_quo, min))
     tibble(min2 = df$out)

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM