0

I have a data frame with yes/no values for different days and hours. For each day, I want to get a total number of hours where I have data, as well as the total number of hours where there is a value of Y.

df <- data.frame(day = c(1,1,1,2,2,3,3,3,3,4), hour = c(1,2,3,1,2,1,2,3,4,1), YN = c("Y","Y","Y","Y","Y","Y","N","N","N","N")) df %>% group_by(day) %>% summarise(tot.hour = n(), totY = WHAT DO I PUT HERE?) 

1 Answer 1

4

Using boolean then add it up

df %>% group_by(day) %>% dplyr::summarise(tot.hour = n(), totY = sum(YN=='Y')) # A tibble: 4 x 3 day tot.hour totY <dbl> <int> <int> 1 1 3 3 2 2 2 2 3 3 4 1 4 4 1 0 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.