a <- data.frame(
variable=c("a","b","c"),
date=c("2015-01-01","2015-01-01","2015-01-02"),
value=c(1,2,3))
我明白
recast(a,date~variable,sum,fill=10000)
Using variable, date as id variables
> date a b c
>1 2015-01-01 1 2 10000
>2 2015-01-02 10000 10000 3
但是
recast(a,date~variable,sum,fill=NA)
Using variable, date as id variables
Error in vapply(indices, fun, .default) : values must be type 'logical', but FUN(X[[1]]) result is type 'double'
为什么会这样?
来自帮助
fill : value with which to fill in structural missings, defaults to value from applying fun.aggregate to 0 length vector
在这种情况下,当缺少值时,我希望 sum
为 NA
。
最佳答案
指定您要使用的 NA
类型,它应该可以工作:
recast(a, date ~ variable, sum, fill = NA_real_)
# Using variable, date as id variables
# date a b c
# 1 2015-01-01 1 2 NA
# 2 2015-01-02 NA NA 3
关于reshape::recast 不会使用 fill=NA 求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823030/