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

number of times specific value in each column R

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))

enter image description here

I want to find number of times the word "FALSE" appears in each column and group and return a df

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

enter image description here

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?

>Solution :

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)))
...
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