0

I have a dataframe with different counts of 1 in each column, like this:

dat <- 
  data.frame('col1' = rep(c(0, 1), 6), 
             'col2' = rep(c(0:2), 4), 
             'col3' = rep(c(0:3), 3))

I'm trying to use dplyr::summarise_each to do the following programatically:

dat_new <- 
  data.frame(tally(dat, col1 == 1), # or count()
             tally(dat, col2 == 1), 
             tally(dat, col3 == 1))

to give, for each column, counts of where the condition rowvalue == 1 are met (in this case, 6, 4, and 3, respectively). However, I'm really struggling trying to do something that I think should be very easy, I would assume using dplyr::summarise_each(). All the help I've found online usually requires some pre-grouping, but that's not necessary in this case. Many thanks for your help

Jonny
  • 2,703
  • 2
  • 27
  • 35
  • 1
    I don't think `dplyr` is necessary. How about `lapply(dat, function(x) sum(x==1))`? –  Aug 09 '16 at 15:39
  • 5
    `summarise_each(dat, funs(sum(. == 1)))` or `colSums(dat == 1)` – talat Aug 09 '16 at 15:40
  • Thank you both - all 3 suggestions work very well. It was the the need to use `sum` instead of `count`/`tally`... shame, because `count` in particular has some nice in-built functionality – Jonny Aug 09 '16 at 15:47
  • Possible duplicate of [Summarizing multiple columns with dplyr?](http://stackoverflow.com/questions/21644848/summarizing-multiple-columns-with-dplyr) – ArunK Aug 10 '16 at 13:09

0 Answers0