Home > Back-end >  R function to split a dataframe into multiple dataframe based on their index
R function to split a dataframe into multiple dataframe based on their index

Time:04-14

Suppose I have a dataframe with 100 rows of data. I want split the dataframe into multiple dataframe each containing 10 rows. For example, the first separated dataframe has index from 1 to 10, and the second dataframe will have an index from 2 to 11, and the third dataframe will have an index from 3 to 12, and so on. I will use this dataframe to make further analysis.

I have tried using the split() function, but I could only figure out a way to split the dataframe evenly. I thought using a loop would simplify it, but I haven't been able to make it work.

iterator <- seq(1,100,10)
for (i in iterator){
  df[[i 1]] <- df[1:100,]
}

I tried something like this, but it did not show me the desired output.

CodePudding user response:

We can use slide

library(slider)
library(dplyr)
library(purrr)
slide(seq_len(nrow(df)), .f = ~ .x, .after = 9) %>%
    keep(~ length(.x) == 10) %>%
    map(~  df %>% 
          slice(.x))

-testing

> slide(1:15, .f = ~ .x, .after = 9) %>%
    keep(~length(.x) == 10)
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[2]]
 [1]  2  3  4  5  6  7  8  9 10 11

[[3]]
 [1]  3  4  5  6  7  8  9 10 11 12

[[4]]
 [1]  4  5  6  7  8  9 10 11 12 13

[[5]]
 [1]  5  6  7  8  9 10 11 12 13 14

[[6]]
 [1]  6  7  8  9 10 11 12 13 14 15

and with mtcars dataset

slide(1:15, .f = ~ .x, .after = 9) %>%
   keep(~ length(.x) == 10) %>%
   map(~ mtcars %>% slice(.x))
[[1]]
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

[[2]]
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4

[[3]]
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE        16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3

[[4]]
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE        16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL        17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3

[[5]]
                   mpg cyl  disp  hp drat   wt  qsec vs am gear carb
Hornet Sportabout 18.7   8 360.0 175 3.15 3.44 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.19 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.15 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
Merc 280C         17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
Merc 450SE        16.4   8 275.8 180 3.07 4.07 17.40  0  0    3    3
Merc 450SL        17.3   8 275.8 180 3.07 3.73 17.60  0  0    3    3
Merc 450SLC       15.2   8 275.8 180 3.07 3.78 18.00  0  0    3    3

[[6]]
                    mpg cyl  disp  hp drat   wt  qsec vs am gear carb
Valiant            18.1   6 225.0 105 2.76 3.46 20.22  1  0    3    1
Duster 360         14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
Merc 240D          24.4   4 146.7  62 3.69 3.19 20.00  1  0    4    2
Merc 230           22.8   4 140.8  95 3.92 3.15 22.90  1  0    4    2
Merc 280           19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
Merc 280C          17.8   6 167.6 123 3.92 3.44 18.90  1  0    4    4
Merc 450SE         16.4   8 275.8 180 3.07 4.07 17.40  0  0    3    3
Merc 450SL         17.3   8 275.8 180 3.07 3.73 17.60  0  0    3    3
Merc 450SLC        15.2   8 275.8 180 3.07 3.78 18.00  0  0    3    3
Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.25 17.98  0  0    3    4

Or using base R with embed

lapply(asplit(embed(seq_len(nrow(df)), 10)[, 10:1], 1)
    function(x) df[x,])
  • Related