I have:
library(tidyverse)
df <- tibble(one=c(1,1,1,2,2,2,3,3),
log1 = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE),
log2 = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
log3 = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE))
I want to find number of times the word "FALSE" appears in each column and group and return a df
I have tried map_df(df, function(x) sum(x==FALSE))
and
df %>%
group_by(one) %>%
map_df( function(x) sum(x==FALSE))
but they do not break into separate groups.
this also errors out
df %>%
group_by(one) %>%
summarise( function(x) sum(x==FALSE))
Any suggestions?
CodePudding user response:
You can use across
to work on multiple columns
library(dplyr)
df %>%
group_by(one) %>%
summarise(across(starts_with("log"), function(x) sum(x==F)))
# A tibble: 3 × 4
one log1 log2 log3
<dbl> <int> <int> <int>
1 1 1 1 1
2 2 3 3 3
3 3 0 2 1
A nice and a little shorter way to do the logic is to use the booleans directly, as mentioned by @RuiBarradas
...
summarise(across(starts_with("log"), function(x) sum(!x)))
...