使用汇总函数与dplyr包进行sumIF

时间:2015-01-30 13:02:19

标签: r statistics

我正在使用dplyr在我的数据框上创建一个sumIF函数。但是,它没有给我所需的输出:

> dput(sys)
structure(list(NUMERIC = c(244L, 24L, 1L, 2L, 4L, 111L, 23L, 
2L, 3L, 4L, 24L), VAL = c("FALSE", "FALSE", "TES", "TEST", "TRUE", 
"TRUE", "TRUE", "asdfs", "asdfs", "safd", "sd"), IDENTIFIER = c(99L, 
99L, 98L, 98L, 99L, 99L, 99L, 13L, 13L, 99L, 12L)), .Names = c("NUMERIC", 
"VAL", "IDENTIFIER"), row.names = c(NA, 11L), class = c("grouped_dt", 
"tbl_dt", "tbl", "grouped_dt", "tbl_dt", "tbl", "data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000000100788>, sorted = c("VAL", 
"IDENTIFIER"), vars = list(VAL, IDENTIFIER))
> 
> 
> sys <- group_by(sys, VAL, IDENTIFIER)
> df.summary <- summarise(sys, 
+                         numeric = sum(NUMERIC)
+ )
> 
> (df.summary)
  numeric
1     442

我想要的结果应该是这样的:

snapshot of data and result

关于我做错了什么的建议?

1 个答案:

答案 0 :(得分:1)

plyrdplyr一起加载时,可能会发生这种情况。您可以在新的R会话中执行此操作或使用

 dplyr::summarise(sys, 
                     numeric = sum(NUMERIC)
  )