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

Looping a pipe through columns of a tibble

I have a tibble with 20 variables. So far I’ve been using this pipe to find out which values appear more than once in a single column

as_tibble(iris) %>% group_by(Petal.Length) %>% summarise(n=sum(n())) %>% filter(n>1)

I was wonering if I could write a line that could loop this through all the columns and return 20 different tibbles (or as many as I need in the future) in the same way the pipe above would return one tibble. I have tried writing my own loops but I’ve had no success, I am quite new.

The iris example dataset has 5 columns so feel free to give an answer with 5 columns.

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

Thank you!

>Solution :

library(dplyr)

col_names <- colnames(iris)

lapply(
  col_names,
  function(col) {
    iris %>%
      group_by_at(col) %>%
      summarise(n = n()) %>% 
      filter(n > 1)
  }
)
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