【问题标题】:error in a pmap Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "character"pmap 中的错误 UseMethod(“filter_”)中的错误:没有适用于“filter_”的方法应用于“字符”类的对象
【发布时间】:2020-04-10 05:09:40
【问题描述】:

当我将输入变量作为数据框输入时,我编写了一个运行良好的函数。但是当我想使用 pmap 将输入作为数据框列表输入时,我收到以下错误:

Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "character"

这是导致错误的函数的数据和第一部分,我在此处未显示的部分函数中使用了 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)

但是当我在这个函数上调用 pmap 时:

df <- tibble::tibble(x = x %>% group_by(strata) %>% nest(),
                     y = y %>% group_by(strata) %>% nest(),
                     a = a)
pmap(df, example, d= 0.1)

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

【问题讨论】:

标签: r purrr pmap


【解决方案1】:

pmap,df 的输入数据帧格式不正确时,正如 CLedbetter 在其有用的答案中提到的那样,会出现此错误。 pmap 期望 df 仅具有其正在操作的函数已知的列。 为此,我用inner_join 编辑了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)

【讨论】:

    【解决方案2】:

    我不相信df 正在创建您希望它创建的df。我相信这可以满足您的要求……如果我正确理解了这个问题。但是 y 没有在您的函数中的任何地方使用,所以我不清楚它的目的是什么。我相信还有更好的方法可以使用mapnest 来做到这一点,但我再次不确定您要做什么。

    library(tidyverse)
    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(a = 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)
    #> # A tibble: 1 x 3
    #>      x3 avg_revenue revenue
    #>   <int>       <dbl>   <int>
    #> 1     1           5      10
    df <- bind_cols(x, select(y, rate)) %>% 
      group_by(strata) %>% 
      nest(x = c(x1, x2, x3), 
           y = c(rate)) %>% 
      bind_cols(a) %>% ungroup()
    pmap(select(df, -strata), example)
    #> [[1]]
    #> # A tibble: 0 x 3
    #> # … with 3 variables: x3 <int>, avg_revenue <dbl>, revenue <int>
    #> 
    #> [[2]]
    #> # A tibble: 0 x 3
    #> # … with 3 variables: x3 <int>, avg_revenue <dbl>, revenue <int>
    #> 
    #> [[3]]
    #> # A tibble: 1 x 3
    #>      x3 avg_revenue revenue
    #>   <int>       <dbl>   <int>
    #> 1     1           4       4
    #> 
    #> [[4]]
    #> # A tibble: 1 x 3
    #>      x3 avg_revenue revenue
    #>   <int>       <dbl>   <int>
    #> 1     1           6       6
    pmap_dfr(select(df, -strata), example, d = 0.1, .id = 'strata')
    #> # A tibble: 2 x 4
    #>   strata    x3 avg_revenue revenue
    #>   <chr>  <int>       <dbl>   <int>
    #> 1 3          1           4       4
    #> 2 4          1           6       6
    

    reprex package (v0.3.0) 于 2019 年 12 月 17 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-28
      • 2019-04-07
      • 2023-04-06
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 2022-12-15
      相关资源
      最近更新 更多