How to split a big data frame into smaller ones in R?

R ProgrammingServer Side ProgrammingProgramming

Dealing with big data frames is not an easy task therefore we might want to split that into some smaller data frames. These smaller data frames can be extracted from the big one based on some criteria such as for levels of a factor variable or with some other conditions. This can be done by using split function.

Example

Consider the below data frame −

> set.seed(1)
> Grades<-rep(c("A","B","C","D","E"),times=10)
> Age<-sample(1:30,50,replace=TRUE)
> Category<-sample(1:10,50,replace=TRUE)
> df<-data.frame(Grades,Age,Category)
> head(df,20)
  Grades Age Category
1    A    25    6
2    B     4    9
3    C     7    8
4    D     1    9
5    E     2    7
6    A    29    8
7    B    23    6
8    C    11   10
9    D    14    7
10   E    18    3
11   A    27   10
12   B    19    6
13   C     1    8
14   D    21    2
15   E    21    2
16   A    10    6
17   B    22    6
18   C    14    1
19   D    10    3
20   E     7    3

Splitting the data frame on the basis of grades −

> Split_based_on_Grades<-split(df,f=df$Grades)
> Split_based_on_Grades
$A
    Grades Age Category
1     A    25    6
6     A    29    8
11    A    27   10
16    A    10    6
21    A     9    8
26    A    25    7
31    A    10    9
36    A     1    1
41    A    10    7
46    A    20   10
$B
    Grades Age Category
2     B     4    9
7     B     23   6
12    B     19   6
17    B     22   6
22    B     15   6
27    B     14   1
32    B     30   7
37    B     20   5
42    B      6   7
47    B     26  10
$C
    Grades Age Category
3     C      7   8
8     C     11  10
13    C      1   8
18    C     14   1
23    C     21   7
28    C      5   4
33    C     25   4
38    C      3   6
43    C     15   3
48    C     12   7
$D
    Grades Age Category
4     D      1   9
9     D     14   7
14    D     21   2
19    D     10   3
24    D      5   6
29    D      5   8
34    D     12   7
39    D      6   1
44    D     20   6
49    D     25   3
$E
    Grades Age Category
5     E      2   7
10    E     18   3
15    E     21   2
20    E      7   3
25    E      9   8
30    E      2   9
35    E     15   6
40    E     10   9
45    E     28   2
50    E     23   2 

Now we can access each grade as shown below −

> Split_based_on_Grades[[1]]
   Grades Age Category
1     A    25   6
6     A    29   8
11    A    27  10
16    A    10   6
21    A     9   8
26    A    25   7
31    A    10   9
36    A     1   1
41    A    10   7
46    A    20  10
> Split_based_on_Grades[[4]]
   Grades Age Category
4     D     1    9
9     D    14    7
14    D    21    2 
19    D    10    3
24    D     5    6
29    D     5    8
34    D    12    7
39    D     6    1
44    D    20    6
49    D    25    3
raja
Published on 10-Aug-2020 13:05:47
Advertisements