ntile()

ntile()

The ntile() function can be used to create equal sized groups (n-tiles) out of a quantitative variable. It can be modified to make any number of groups. For instance, if you take a quantitative variable such as Height from the Fingers data frame, you can split into two equal groups, to create a "short" and a "tall" group. In Example 1 below, you can see in the output that it does this by assigning all the Height values below the median as "1" and all the Height values above the median as "2". 

Note that R also automatically codes the numbered groups as a quantitative variable (an integer), so you may need to apply the factor() function if you would like the values to be coded as a categorical variable (this is also helpful if you would like to relabel the numbers as characters (e.g., changing "1" to "short", and "2" to "tall")). See Example 3.

Example 1:

# creates two equal sized groups and saves it as a new variable (Height_2)
Fingers$Height_2 <- ntile(Fingers$Height, 2)
head(select(Fingers, Height, Height_2))

Example output:

Example 2:

# creates four equal sized groups and saves it as a new variable (Height_4)
Fingers$Height_4 <- ntile(Fingers$Height4)
head(select(FingersHeightHeight_4))

Example output:

Example 3: 

# creates two equal sized groups
Fingers$Height_2 <- ntile(Fingers$Height2)
# designates the new variable as a factor and labels the group numbers
Fingers$Height_2_factor <- factor(Fingers$Height_2, levels = c(1, 2), labels = c("short",  "tall"))
# check the data frame
head(select(FingersHeightHeight_2, Height_2_factor))

Example output:


    • Related Articles

    • factor()

      The factor() function will convert a quantitative variable into a factor (a categorical variable). This is often needed when categorical variables are dummy coded as numeric values so R treats them as a quantitative variable. For example, when a ...