jeudi 12 septembre 2019

In IF statement, condition on the number of groups AND the number of observations per group

How do I in a IF statement condition on the number of groups AND the number of observations per group? I.e. if >=4 groups are present AND with >=2 observations, do something.

The first part is less tricky, it's the second part I'm really struggling with.

library(tidyverse)
data(mtcars)

set.seed(123)

mtcars <- mtcars %>% rownames_to_column("type")
mtcars$brand <- stringr::str_split_fixed(mtcars$type, " ", 2)[,1]
mtcars <- mtcars[mtcars$brand %in% c("Merc","Mazda","Hornet","Toyota"),]

mtcars_ls <- vector("list",5)
for(n in 1:5){ mtcars_ls[[n]] <- mtcars[mtcars$type %in% sample(mtcars$type, size=15, replace=T),]}

for(i in seq_along(mtcars_ls)) {
  if( length(unique(mtcars_ls[[i]]$brand)) >= 4 ) { next } 
  else { mtcars_ls[[i]] <- NULL } 
}

mtcars_ls
[[1]]
                type  mpg cyl  disp  hp drat    wt  qsec vs am gear carb  brand
1          Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  Mazda
2      Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  Mazda
5  Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 Hornet
9           Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2   Merc
10          Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4   Merc
11         Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4   Merc
12        Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3   Merc
14       Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3   Merc
20    Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Toyota
21     Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1 Toyota

[[2]]
                type  mpg cyl  disp  hp drat    wt  qsec vs am gear carb  brand
1          Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  Mazda
2      Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  Mazda
5  Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 Hornet
8          Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2   Merc
11         Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4   Merc
12        Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3   Merc
13        Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   Merc
20    Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Toyota
21     Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1 Toyota

[[3]]
                type  mpg cyl  disp  hp drat    wt  qsec vs am gear carb  brand
1          Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  Mazda
2      Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  Mazda
4     Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 Hornet
5  Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 Hornet
8          Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2   Merc
9           Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2   Merc
10          Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4   Merc
12        Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3   Merc
13        Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   Merc
14       Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3   Merc
20    Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Toyota
21     Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1 Toyota

[[4]]
                type  mpg cyl  disp  hp drat    wt  qsec vs am gear carb  brand
1          Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  Mazda
2      Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  Mazda
4     Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 Hornet
5  Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 Hornet
8          Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2   Merc
9           Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2   Merc
10          Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4   Merc
11         Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4   Merc
13        Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   Merc
14       Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3   Merc
20    Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Toyota


The above code removes any list with less than 4 car brands present. What I really want it to do is to remove lists with less than 4 car brands AND with 2 observations per car brand. In the above example, this would only leave mtcars_ls[[3]].

Aucun commentaire:

Enregistrer un commentaire