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

compact way to create filtered dataframes cycling through different values (R)

I have a dataframe with a column named CHR which has discrete values from 1 to 18 (1, 2, 3 …)

I want to subset the dataframes for each value of CHR. So far my code (working) looks like this:

CH1<-boxplot %>% filter(CHR == "1")
CH2<-boxplot %>% filter(CHR == "2")
CH3<-boxplot %>% filter(CHR == "3")
               .
               .
               .
CH18<-boxplot %>% filter(CHR == "18")

It does get the job done, but I’m very annoyed whenever my code looks like that. I want to learn one "proper" way so I can apply it to multiple other similar cases.

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

>Solution :

You have a few options:

1. Write a function, although you will still have many lines, they are condensed lines.

bx_filter <- function(boxplot, chr) {
  boxplot %>% filter(CHR == chr)
}

CH1 <- bx_filter("1")
CH2 <- bx_filter("2")

2. Use split(), where you’ll get a list and each element of the list has the data frames you’re looking for

split(boxplot, boxplot$CHR)

3. A combo of map() and assign(), although it’s generally frowned upon to write to the Global environment in ways similar to this

unique(boxplot$CHR) %>%
  map(function(chr) {
    assign(paste0('CH', chr), boxplot %>% filter(CHR == chr), envir = .GlobalEnv)
  })
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