当我将输入变量作为数据帧输入时,我编写了一个运行良好的函数。但是,当我想使用pmap输入输入作为数据帧列表时,出现以下错误:
Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "character"
这里是导致错误的数据和函数的第一部分,我在此处未显示的部分函数中使用y和一个参数:
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)
我收到上述错误。
答案 0 :(得分:2)
我不认为df
正在创建您要创建的df
。我相信这可以满足您的需求...如果我正确理解了这个问题。但是y
在函数的任何地方都没有使用,因此我不清楚它的目的是什么。我相信还有一种更好的方法可以使用map
和nest
来执行此操作,但是我不确定您要做什么。
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答案 1 :(得分:0)
当pmap
,df
的输入数据帧的格式不正确时,就会出现此错误,正如CLedbetter在他们的有用答案中也提到的那样。 pmap
期望df
仅包含对其所操作的函数已知的列。
为此,我用df
编辑了inner_join
,然后仍然有函数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)