base R

R Base Gems

Some Gems from R Base

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.

Introduction to the Subset function

Although I’m a big fan of the tidyverse philosophy of handling and wrangling data, one must admit that there are quit powerful functions in base R. One of theses functions is subset() which returns a dataframe according to some defined subsetting properties. Let’s dive into one example using the simple mtcars data: head(mtcars) # A quick look at the mtcars data ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.