How to split a dataframe in R?

In this tutorial we’ll work with the ChickWeight data. This database exhibits information on chickens’ weights according to 4 different diets.

head(ChickWeight)
##   weight Time Chick Diet
## 1     42    0     1    1
## 2     51    2     1    1
## 3     59    4     1    1
## 4     64    6     1    1
## 5     76    8     1    1
## 6     93   10     1    1

Suppose, we want to split our overal dataframe according to each type of diet. There is a simple function called split() that allows us to do that. Note that the splitting argument must be a factor object. Let’s check this condition.

class(ChickWeight$Diet)
## [1] "factor"

Indeed, the Diet variable is a factor. Now, we split our dataframe :

splitted_data <- split(ChickWeight, ChickWeight$Diet)

In this context, we can assign a name to each splitted data:

Diet1 <- splitted_data$`1`  # The 1, 2, 3, 4 represent the diet factor levels

Diet2 <- splitted_data$`2`

Diet3 <- splitted_data$`3`

Diet4 <- splitted_data$`4`

Finally, let’s check our result by printig the first values of Diet 3 and Diet 4:

head(Diet3)
##     weight Time Chick Diet
## 341     42    0    31    3
## 342     53    2    31    3
## 343     62    4    31    3
## 344     73    6    31    3
## 345     85    8    31    3
## 346    102   10    31    3
head(Diet4)
##     weight Time Chick Diet
## 461     42    0    41    4
## 462     51    2    41    4
## 463     66    4    41    4
## 464     85    6    41    4
## 465    103    8    41    4
## 466    124   10    41    4

Great, it’s perfectly working.

Avatar
Mohamed El Fodil Ihaddaden
Ph.D candidate in Economics.

My research interests include Performance Management, Efficiency Analysis and Experimental Economics.

Related