reshape::recast 不会使用 fill=NA 求和
reshape::recast won't use fill=NA for sum
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
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