Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to count non zero values in each category

this is my data

df <- structure(list(team_3_F = c("team ", "team ", "site", "site", 
"team ", "team ", "newyorkish", "newyorkish", "team ", "team ", 
"newyorkish", "newyorkish", "browingal ", "browingal ", "site", 
"site", "browingal ", "browingal ", "browingal ", "browingal ", 
"team ", "team ", "team ", "team ", "team ", "team ", "team ", 
"team ", "team ", "team ", "site", "site", "browingal ", "browingal ", 
"browingal ", "browingal ", "browingal ", "browingal ", "browingal ", 
"browingal ", "browingal ", "browingal ", "team ", "team ", "team ", 
"team ", "newyorkish", "newyorkish", "browingal ", "browingal ", 
"newyorkish", "newyorkish", "browingal ", "browingal ", "team ", 
"team ", "browingal ", "browingal ", "team "), name = c("AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", 
"AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", "BBB_US", "AAA_US", 
"BBB_US", "AAA_US"), value = c(0L, 0L, 0L, 8L, 1L, 0L, 11L, 0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 45L, 
0L, 0L, 0L, 18L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 3L, 
0L, 2L, 0L, 2L, 1L, 0L, 4L, 0L, 88L, 0L, 0L, 1L, 5L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 19L)), row.names = c(NA, -59L), class = "data.frame")

I am trying to identify the non-zero per each group and so I should have an output like this

browingal   AAA_US  1
browingal   BBB_US  7
newyorkish  AAA_US  4
newyorkish  BBB_US  0
site        AAA_US  0
site        BBB_US  1
team        AAA_US  6
team        BBB_US  0

I tried to categorize it but I could not figure it out

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df %>% group_by(name) %>% summarise_each(function(x) min(x[x != 0]),value)

>Solution :

You can use the following solution. As per documentation:

mutate_each() and summarise_each are deprecated in favor of new
across function that works with summarize() & mutate().

library(dplyr)

df %>%
  group_by(team_3_F, name) %>%
  summarise(across(value, ~ sum(.x != 0)))

# A tibble: 8 x 3
# Groups:   team_3_F [4]
  team_3_F     name   value
  <chr>        <chr>  <int>
1 "browingal " AAA_US     1
2 "browingal " BBB_US     7
3 "newyorkish" AAA_US     4
4 "newyorkish" BBB_US     0
5 "site"       AAA_US     0
6 "site"       BBB_US     1
7 "team "      AAA_US     6
8 "team "      BBB_US     0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading