【问题标题】:Call custom function with if statement in the summarize function in dplyr在 dplyr 的汇总函数中使用 if 语句调用自定义函数
【发布时间】:2014-10-14 13:59:43
【问题描述】:

我需要调用一个自定义函数来进行一些计算。在这个函数中,有一个 if 语句来检查输入值。但是我的代码没有返回我期望的值。

创建了一个测试data.frame

library(dplyr)
df <- expand.grid(x = 2:4, y = 2:4, z = 2:4)
df$value <- df$x
df <- df%>% tbl_df %>% group_by(x, y)

test_fun1 只返回所有值的总和

test_fun1 <- function(value)
{
    return(sum(value))
}
df %>% summarize(t  = test_fun1(value))

test_fun1 按我的预期返回结果

Source: local data frame [4 x 3]
Groups: x

  x y t
1 1 1 2
2 1 2 2
3 2 1 4
4 2 2 4

然后我添加一个 if 语句来检查所有值是否相等。

test_fun2 <- function(value)
{
    if (all(value == 2))
    {
        return (NA)
    }
    return(sum(value))
}
df  %>% summarize(t  = test_fun2(value))

但如果值大于 2,test_fun2 返回 TRUE

Source: local data frame [9 x 3]
Groups: x

  x y    t
1 2 2   NA
2 2 3   NA
3 2 4   NA
4 3 2 TRUE
5 3 3 TRUE
6 3 4 TRUE
7 4 2 TRUE
8 4 3 TRUE
9 4 4 TRUE

对于其他值,对于 test_fun3 的其他值,结果与预期一致。

test_fun3 <- function(value)
{
    if (all(value != 3))
    {
        return(sum(value))
    }
    return (NA)

}
df  %>% summarize(t  = test_fun3(value))

我可以得到 4 或 5 的类似结果

Source: local data frame [9 x 3]
Groups: x

  x y  t
1 2 2  6
2 2 3  6
3 2 4  6
4 3 2 NA
5 3 3 NA
6 3 4 NA
7 4 2 12
8 4 3 12
9 4 4 12

在我的真实数据中,我得到了非 NA 测试的 FALSE,但无法在此处创建重现示例。

关于这个问题的任何想法?感谢您的任何建议。

sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.2

loaded via a namespace (and not attached):
[1] assertthat_0.1.0.99 magrittr_1.0.1      parallel_3.1.0     
[4] Rcpp_0.11.1         tools_3.1.0        

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    问题很明显,mutate 试图从第一个分配中确定列的类,并将该类应用于所有其他组。 NA 的类是(不幸的是)logical。更多详情可以看这里https://github.com/hadley/dplyr/issues/299

    我建议您通过分配一个已转换的NA 来解决此问题。另见? NA

    test_fun2 <- function(value) {
      if (all(value == 2)) {
        return (NA_integer_)
      }
      return(sum(value))
    }
    
    df  %>% summarize(t  = test_fun2(value))
    
    Source: local data frame [9 x 3]
    Groups: x
    
      x y  t
    1 2 2 NA
    2 2 3 NA
    3 2 4 NA
    4 3 2  9
    5 3 3  9
    6 3 4  9
    7 4 2 12
    8 4 3 12
    9 4 4 12
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-25
      • 2022-06-10
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多