r - 如何使用 dcast 正确实现用户定义的聚合函数

标签 r dataframe reshape2

我有一个像这样的数据框

df<-data.frame(date=c(rep("1/27/2010",times=30)),
           loc1=c(rep(9:13,each=6)),
           loc2=c(rep(c("N","E","W"),each=2)),
           loc3=c(rep(c(1,2))),
           tr1=c(rep(c(0,1),each=15)),
           tr2=c(0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1),
           tr3=c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4),
           Birth=c(sample(c("early","late"),30,replace=TRUE,prob=c(0.5,0.5))),
           Species=c(rep(c("A","B"),times=15)),
           Status=c(sample(c(0,1),30,replace=TRUE,prob=c(0.7,0.3))))

df<-rbind(df,df)

我想为 loc3 的每个值创建单独的列,其中行由 loc1、loc2、tr1、tr2、tr3、出生和物种定义。我想“计算”共享这些值的所有观察值的状态,并按 loc3 对计数进行分组。

我计划使用 reshape2 包中的 dcast。

我编写了一个函数来执行我想要的“计数”。我是 R 新手,虽然我确信有一个函数可以做到这一点,但我无法立即找到它,考虑到任务的简单性,尝试自己编写脚本似乎是一个值得的练习。

      d.count<-function(x){
  j=0
  for (i in 1:length(x))
    if (is.na(x{i])){
      j<-j+0
    }else if(x[i]==0){
      j<-j+1
    } else if(x[i]==1){
      j<-j+0
    }
  return(j)
}

0 应该增加计数,而 1 和 NA 则不应增加。

所以

df_1<-dcast(df,date+loc1+loc2+tr1+tr2+tr3+Birth+Species~loc3,value.var="Status",fun.aggregate=d.count)

我收到错误

Error in if (is.na(x[i])) { : argument is of length zero

这让我觉得我不明白 dcast 如何对待 fun.aggregate...

感谢您的帮助! -JJE

最佳答案

为什么不使用 tabulate 函数进行类似的操作

require(reshape2)
dcast(df, ... ~ loc3, value.var = "Status", fun.aggregate = tabulate)

##         date loc1 loc2 tr1 tr2 tr3 Birth Species 1 2
## 1  1/27/2010    9    E   0   0   1 early       A 0 0
## 2  1/27/2010    9    E   0   0   1 early       B 0 0
## 3  1/27/2010    9    N   0   0   1 early       B 0 0
## 4  1/27/2010    9    N   0   0   1  late       A 0 0
## 5  1/27/2010    9    W   0   0   1 early       B 0 0
## 6  1/27/2010    9    W   0   0   1  late       A 0 0
## 7  1/27/2010   10    E   0   1   2  late       A 0 0
## 8  1/27/2010   10    E   0   1   2  late       B 0 2
## 9  1/27/2010   10    N   0   0   1  late       A 0 0
## 10 1/27/2010   10    N   0   1   2  late       B 0 2
## 11 1/27/2010   10    W   0   1   2  late       A 0 0
## 12 1/27/2010   10    W   0   1   2  late       B 0 0
## 13 1/27/2010   11    E   0   1   2  late       A 0 0
## 14 1/27/2010   11    E   1   0   3 early       B 0 2
## 15 1/27/2010   11    N   0   1   2 early       B 0 0
## 16 1/27/2010   11    N   0   1   2  late       A 0 0
## 17 1/27/2010   11    W   1   0   3  late       A 0 0
## 18 1/27/2010   11    W   1   0   3  late       B 0 2
## 19 1/27/2010   12    E   1   0   3 early       B 0 0
## 20 1/27/2010   12    E   1   0   3  late       A 0 0
## 21 1/27/2010   12    N   1   0   3 early       A 2 0
## 22 1/27/2010   12    N   1   0   3 early       B 0 2
## 23 1/27/2010   12    W   1   0   4 early       A 0 0
## 24 1/27/2010   12    W   1   1   4 early       B 0 0
## 25 1/27/2010   13    E   1   1   4 early       B 0 0
## 26 1/27/2010   13    E   1   1   4  late       A 0 0
## 27 1/27/2010   13    N   1   1   4  late       A 0 0
## 28 1/27/2010   13    N   1   1   4  late       B 0 2
## 29 1/27/2010   13    W   1   1   4 early       A 0 0
## 30 1/27/2010   13    W   1   1   4 early       B 0 2

编辑

如果你想计算 0 的个数,例如:

dcast(df, ... ~ loc3, value.var = "Status", 
         fun.aggregate = function(x) sum(x == 0, na.rm = TRUE))

关于r - 如何使用 dcast 正确实现用户定义的聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17178904/

相关文章:

r - 用 reshape2:::dcast 转置 data.table

r - 加速 R 代码以基于其他数据帧用字符串填充向量

python - Pandas,如何将系列添加到 DataFrame 列,其中系列索引与 DataFrame 列匹配?

r - 基于 R 中的另一列对数据框中的列求和

r 中多列的 rowsum

python - 在 Pandas 的数据框中聚合列表

r - 使用 reshape 查找配对事件

r - 变量 R 之间的转换

R:ggplot2 绘制按工作日分面的每小时数据

将带有日语字符的csv文件读入R