我需要调用自定义函数进行一些计算。在此函数中,有一个if语句可检查输入值。但是我的代码没有返回我期望的值。
创建一个测试数据框架
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
问题显然是,它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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句