reshape::recast 不会使用 fill=NA 求和

标签 r reshape reshape2

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

在这种情况下,当缺少值时,我希望 sumNA

最佳答案

指定您要使用的 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/

相关文章:

r - 如何在不执行 sum 或 mean 之类的函数的情况下 reshape data.table(从长到宽)?

r - GoogleVis 和 Shiny

c# - 使用 R.Net 版本 1.5.5 创建 REngine 实例

r - 系数表在秩缺陷拟合中没有 NA 行;如何插入它们?

r - 'colsplit' 函数出错?

pandas - Python - reshape 、旋转、unstack - 多重索引

r - 将数据从多行转换为多列

从嵌套数据框/小标题运行多个简单线性回归

r - 错误 : package or namespace load failed for ‘data.table’ in library. dynam(lib, package, package.lib): 找不到共享对象 ‘datatable.so’

r - 带有两个测量变量和一个因子的误差线的 ggplot