# The slice() function in R: How to use slice() in R

The slice() function in R is a powerful function that helps us to subset rows. Please follow our instructions below if you are new to this because we will show you more details about its syntax, application, and how to use it in your R program.

## Slice() function in R

### What does the slice() function do in R?

The idea of the slice() function is to subset a row. Or, to be more understandable, it will give only the data of the row that we need. By doing this, data is much more manageable and easy to keep track if you work with large data sets. Let’s take a look at this small example:

This dataset demonstrates the sales of Fairtrade-labelled coffee in 1999 and 2004:

dataFrame <- data.frame(
"Coffee" = c("UK", "Switzerland", "Denmark", "Belgium", "Sweden"),
"1999 (millions of euros)" = c(1.5, 3, 1.8, 1, 0.8),
"2004 (millions of euros)" = c(20, 6, 2, 1.7, 1),
check.names = FALSE
)

dataFrame
       Coffee 1999 (millions of euros) 2004 (millions of euros)
1          UK                      1.5                     20.0
2 Switzerland                      3.0                      6.0
3     Denmark                      1.8                      2.0
4     Belgium                      1.0                      1.7
5      Sweden                      0.8                      1.0

For example, we want to get only the data of Belgium, so we will use the slice() function with row 4.

dataFrame %>% slice(4)

Output:

   Coffee 1999 (millions of euros) 2004 (millions of euros)
1 Belgium                        1                      1.7

Syntax:

slice(row)

Parameter:

• row: the row that you want to subset.

### How to use slice() in R?

#### Install the ‘dplyr’ package

The slice() function is part of the ‘dplyr‘ package, so first, we have to install the package before using the function.

Simply type in the console:

install.packages("dplyr")

Next, load the package by typing in:

library("dplyr")

Now everything is set and ready to go.

#### Subset a single row

Example 2:

Let’s say we have this data Frame about the underground system in different cities:

dataFrame <- data.frame(
"City" = c("London", "Paris", "Tokyo", "Washington DC", "Kyoto", "Los Angeles"),
"Date opened" = c(1863, 1900, 1927, 1976, 1981, 2001),
"Kilometers of route" = c(394, 199, 155, 126, 11, 28),
"PAssengers per year (in millions)" = c(775, 1191, 1927, 144, 45, 50),
check.names = FALSE
)

dataFrame
           City Date opened Kilometers of route PAssengers per year (in millions)
1        London        1863                 394                               775
2         Paris        1900                 199                              1191
3         Tokyo        1927                 155                              1927
4 Washington DC        1976                 126                               144
5         Kyoto        1981                  11                                45
6   Los Angeles        2001                  28                                50


If you want to get only the data of 1 city (for example, Paris), use:

dataFrame %>% slice(2)

Output:

   City Date opened Kilometers of route PAssengers per year (in millions)
1 Paris        1900                 199                              1191

#### Subset various rows

If you want to get the data of multiple cities, use:

dataFrame %>% slice(2, 4, 6)

Output:

           City Date opened Kilometers of route Passengers per year (in millions)
1         Paris        1900                 199                              1191
2 Washington DC        1976                 126                               144
3   Los Angeles        2001                  28                                50

#### Subset a range of rows

If you want to subset a range of rows, do as follows:

dataFrame %>% slice(2:5)

Output:

           City Date opened Kilometers of route PAssengers per year (in millions)
1         Paris        1900                 199                              1191
2         Tokyo        1927                 155                              1927
3 Washington DC        1976                 126                               144
4         Kyoto        1981                  11                                45

## Summary

In this tutorial, we have shown you about the slice() function in R and how to apply it in your program. The slice() function is part of the ‘dplyr‘ package, and it helps us to subset rows.

Maybe you are interested:

Posted in R