mrquad mrquad - 28 days ago 14
R Question

Using summarise function to make sumIF with the dplyr package

I am using the

dplyr
to make a sumIF function on my data frame. However, it does not give me the desired output:

> 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


My desired result should look like that:

snapshot of data and result

Any recommendation as to what I am doing wrong?

Answer

This could occur when you have plyr loaded along with dplyr. You can either do this on a new R session or use

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