R pmap错误使用错误方法(“过滤器”):没有适用于';过滤器';适用于“类”的对象;“字符”;

R pmap错误使用错误方法(“过滤器”):没有适用于';过滤器';适用于“类”的对象;“字符”;,r,purrr,pmap,R,Purrr,Pmap,我已经编写了一个函数,当我将输入变量作为数据帧输入时,该函数运行良好。但是,当我想使用pmap以数据帧列表的形式输入输入时,会出现以下错误: UseMethod(“filter”)中的错误:“filter”没有适用于“character”类对象的“filter”方法。 这是导致错误的数据和函数的第一部分,我在函数中未显示的部分使用y和a参数: x <- tibble::tibble(x1 = sample(0:1, 8, replace = TRUE),

我已经编写了一个函数,当我将输入变量作为数据帧输入时,该函数运行良好。但是,当我想使用pmap以数据帧列表的形式输入输入时,会出现以下错误:

UseMethod(“filter”)中的错误:“filter”没有适用于“character”类对象的“filter”方法。

这是导致错误的数据和函数的第一部分,我在函数中未显示的部分使用y和a参数:

x <- tibble::tibble(x1 = sample(0:1, 8, replace = TRUE),
                    x2 = sample(0:25, 8, replace = FALSE),
                    x3 = sample(1:3, 8, replace = TRUE),
                    strata =c("a", "b", "c", "d", "a", "b", "c", "d"))
y <- tibble::tibble(rate = sample(0:1, 8, replace = TRUE),
                    strata =c("a", "b", "c", "d", "a", "b", "c", "d") )

a <- tibble::tibble(sample(10:80, 4, replace = FALSE))
example <- function(x, y, a , d){

  CR <- x %>% filter(x1, x2>0) %>%
    group_by(x3) %>%
    summarise(avg_revenue = mean(x2), revenue = sum(x2))
  return(CR)
}

example(x,y,a, d = 0.1)

x%nest(),
y=y%%>%group_by(地层)%%>%nest(),
a=a)
pmap(df,例如,d=0.1)

我得到了上面提到的错误。

我不相信
df
正在创建您想要创建的
df
。我相信这正是你想要的……如果我理解正确的话。但是,
y
没有在函数中的任何位置使用,因此我不清楚它的用途。我相信还有一种更好的方法可以使用
map
nest
来实现这一点,但我还是不确定您想做什么

库(tidyverse)
x#A tible:1 x 3
#>x3平均收入
#>             
#> 1     1           5      10
df%
组别(阶层)%>%
嵌套(x=c(x1,x2,x3),
y=c(比率))%>%
bind_cols(a)%%>%ungroup()
pmap(选择(df,-地层),示例)
#> [[1]]
#>#tible:0 x 3
#>#…有3个变量:x3,平均收入,收入
#> 
#> [[2]]
#>#tible:0 x 3
#>#…有3个变量:x3,平均收入,收入
#> 
#> [[3]]
#>#A tible:1 x 3
#>x3平均收入
#>             
#> 1     1           4       4
#> 
#> [[4]]
#>#A tible:1 x 3
#>x3平均收入
#>             
#> 1     1           6       6
pmap_dfr(选择(df,-strata),例如,d=0.1,.id='strata')
#>#A tibble:2 x 4
#>地层x3平均收入
#>               
#> 1 3          1           4       4
#> 2 4          1           6       6

由(v0.3.0)于2019-12-17创建此错误,正如CLedbetter在其帮助回答中所述,当
pmap
df
的输入数据帧格式不正确时,会出现此错误
pmap
期望
df
只包含其所操作的函数已知的列。 为此,我用一个
内部连接编辑了
df
,然后我们仍然有
strata
列,这是函数
example()
所不知道的。 正如R中的
pmap
函数帮助中所述,为了使pmap函数忽略函数
example()
未使用的列, 我在
example()
的定义中使用了“…”,以便pmap可以跳过数据帧的第一列
strata
,该列未在函数中使用

因此,更新后的代码将是:

x <- tibble::tibble(x1 = sample(0:1, 8, replace = TRUE),
                    x2 = sample(0:25, 8, replace = FALSE),
                    x3 = sample(1:3, 8, replace = TRUE),
                    strata =c("a", "b", "c", "d", "a", "b", "c", "d"))
y <- tibble::tibble(rate = sample(0:1, 8, replace = TRUE),
                    strata =c("a", "b", "c", "d", "a", "b", "c", "d") )

a <- tibble::tibble(sample(10:80, 4, replace = FALSE))

# Note the addition of the "..." to the function input definition

example <- function(x, y, a , d, ...){

  CR <- x %>% filter(x1, x2>0) %>%
    group_by(x3) %>%
    summarise(avg_revenue = mean(x2), revenue = sum(x2))
  return(CR)
}

example(x,y,a, d = 0.1)

# Note the change in the reformatting of df with an inner_join

df <- inner_join(x %>% group_by(strata) %>% nest(),
                 y %>% group_by(strata) %>% nest(), 
                 by = "strata") %>% rename(x = data.x, y = data.y )

# with these changes pmap produces the output 
pmap(df, example, d= 0.1)

x%nest(),
y%%>%group_by(地层)%%>%nest(),
by=“strata”)%%>%重命名(x=data.x,y=data.y)
#通过这些更改,pmap生成输出
pmap(df,例如,d=0.1)

您能否提供最小可重复性示例的数据?()谢谢你的提示,我已经更新了代码,这样错误就可以重现了。
x <- tibble::tibble(x1 = sample(0:1, 8, replace = TRUE),
                    x2 = sample(0:25, 8, replace = FALSE),
                    x3 = sample(1:3, 8, replace = TRUE),
                    strata =c("a", "b", "c", "d", "a", "b", "c", "d"))
y <- tibble::tibble(rate = sample(0:1, 8, replace = TRUE),
                    strata =c("a", "b", "c", "d", "a", "b", "c", "d") )

a <- tibble::tibble(sample(10:80, 4, replace = FALSE))

# Note the addition of the "..." to the function input definition

example <- function(x, y, a , d, ...){

  CR <- x %>% filter(x1, x2>0) %>%
    group_by(x3) %>%
    summarise(avg_revenue = mean(x2), revenue = sum(x2))
  return(CR)
}

example(x,y,a, d = 0.1)

# Note the change in the reformatting of df with an inner_join

df <- inner_join(x %>% group_by(strata) %>% nest(),
                 y %>% group_by(strata) %>% nest(), 
                 by = "strata") %>% rename(x = data.x, y = data.y )

# with these changes pmap produces the output 
pmap(df, example, d= 0.1)