# Indexing and Slicing Data Frames in R

Last Updated : 21 Dec, 2023

In this article let’s discuss indexing and slicing the Data Frames or how to access elements of a data frame in R Programming Language.

## What is Indexing or accessing?

The process of accessing particular data components or subsets within a vector, matrix, or data frame is called indexing. It enables us to pick, remove, or change particular values or parts of our data based on criteria.

## Indexing or accessing the Data Frame in R

By indexing the Data Frame in R we will get the particular column data. Indexing can be done by specifying column names in square brackets. The syntax for indexing the data frame is-

dataframeName[“columnName”]

## R

 `# create a data frameÂ ` `stats <- ``data.frame``(player=``c``(``'A'``, ``'B'``, ``'C'``, ``'D'``),` `                ``runs=``c``(100, 200, 408, ``NA``),` `                ``wickets=``c``(17, 20, ``NA``, 5))`   `print``(``"stats Dataframe"``)` `stats`   `# fetch data in certain column` `stats[``"runs"``]`

Output:

`  player runs wickets1      A  100      172      B  200      203      C  408      NA4      D   NA       5 stats["runs"]  runs1  1002  2003  4084   NA`

In this example we create a Data Frame “stats” that contains runs scored and wickets taken by a player and perform indexing on the data frame to extract runs scored by players.

## Slicing In R

The process of extracting or accessing particular subsets or sections of a vector, matrix, or data frame depending on predetermined criteria is known as slicing. Using start and end indices, we can choose sequential ranges of elements or subsets of data.

## Slicing or accessing the Data Frame

Slicing the Data Frame gives the required rows and columns. This can be done by three ways. They are listed below-

• Slicing with [ , ]
• Slicing with logical vectors.
• Slicing with subset().

### Slicing or accessing with [ , ]

Slicing the data frame with [ , ] returns data of specified rows and columns. The syntax of this is mentioned below-

dataframeName[ fromRow : toRow , columnNumber]

## R

 `# create a data frameÂ ` `stats <- ``data.frame``(player=``c``(``'A'``, ``'B'``, ``'C'``, ``'D'``),` `                ``runs=``c``(100, 200, 408, ``NA``),` `                ``wickets=``c``(17, 20, ``NA``, 5))`   `print``(``"stats Dataframe"``)` `stats`   `# fetch 2,3 rows and 1,2 columns` `stats[2:3,``c``(1,2)]`   `# fetch 1:3 rows of 1st column` `cat``(``"players - "``)` `stats[1:3,1]`

Output

`[1] "stats Dataframe"  player runs wickets1      A  100      172      B  200      203      C  408      NA4      D   NA       5fetch 2,3 rows and 1,2 columns  player runs2      B  2003      C  408fetch 1:3 rows of 1st column[1] "A" "B" "C"`

In the below code we performed slicing on the data frame to fetch specified rows and columns.

### Slicing with logical vectors

We can perform slicing on the data by specifying the logical conditions. It is used to get the filtered data.

## R

 `# create a data frameÂ ` `stats <- ``data.frame``(player=``c``(``'A'``, ``'B'``, ``'C'``, ``'D'``),` `                ``runs=``c``(100, 200, 408, 23),` `                ``wickets=``c``(17, 20, 3, 5))`   `print``(``"stats Dataframe"``)` `stats`   `# fetch player details who scores ` `# more than 100 runs` `batsmens<-stats[stats\$runs>100,]` `batsmens`

Output

`[1] "stats Dataframe"  player runs wickets1      A  100      172      B  200      203      C  408       34      D   23       5  player runs wickets2      B  200      203      C  408       3`

In this example, we fetch the records of players who scored more than 100 runs.

### Slicing with subset()

We can slice data frames using the subset() method. The subset method accepts the data, filter logic to slice and the columns to fetch. The syntax of slicing with subset is –

subset( x = dataframe, subset = filter_logic, select=c(columnNames))

## R

 `# create a data frameÂ ` `stats <- ``data.frame``(player=``c``(``'A'``, ``'B'``, ``'C'``, ``'D'``),` `                ``runs=``c``(100, 200, 408, 23),` `                ``wickets=``c``(17, 20, 3, 5))`   `print``(``"stats Dataframe"``)` `stats`   `# fetch player details who pick ` `# more than 5 wickets` `subset``(x=stats, subset=wickets>5, select=``c``(player,wickets))`

Output

`[1] "stats Dataframe"  player runs wickets1      A  100      172      B  200      203      C  408       34      D   23       5  player wickets1      A      172      B      20`

In the below code, we fetched the players who picked more than 5 wickets from the data frame stats by slicing the data frame using the subset method.

Previous
Next