How to Retrieve Row Index Number in R


Often you may want to retrieve row index numbers in R to perform specific tasks.

Here are the most common ways to do so:

Method 1: Get Row Numbers of Data Frame

#get all row numbers of data frame
as.numeric(rownames(df))

Method 2: Get Entire Row that Corresponds to Row Index Number

#get entire row with row index number of 3
df[3, ]

Method 3: Get Row with Specific Value in Column

#get row with a value of 95 in 'points' column
df[match(95,df$points),]

The following examples show how to use each of these methods in practice with the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Example 1: Get Row Numbers of Data Frame

We can use the following syntax to get all row numbers in the entire data frame named df:

#get all row numbers of data frame
as.numeric(rownames(df))

[1] 1 2 3 4 5 6 7 8

This returns a vector with numbers 1 through 8, which represent the row numbers for each row in the data frame.

Example 2: Get Entire Row that Corresponds to Row Index Number

We can use the following syntax to get the entire row of the data frame that corresponds to a specific row index number:

#get entire row with row index number of 3
df[3, ]

  team points assists rebounds
3    A     86      31       24

This returns the entire row that corresponds to a row index number of 3.

Note that this returns the values of all columns in the data frame for this particular row.

If you’d like, you can also use a colon to reference a range of row numbers.

For example, you can use the following syntax to return all of the rows between row index number 3 and 6:

#get all rows between index numbers 3 and 6
df[3:6, ]

  team points assists rebounds
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36

Notice that this returns all rows between row index numbers 3 and 6 of the data frame.

Example 3: Get Row with Specific Value in Column

Suppose that we would like to get the row that contains the value 95 in the points column of the data frame.

We can use the following syntax to do so:

#get row with a value of 95 in 'points' column
df[match(95,df$points),]

  team points assists rebounds
5    B     95      34       30

This returns the entire row that contains the value 95 in the points column of the data frame.

Note that if there is no value in the points column with this specific value then nothing will be returned.

Also note that if there are multiple rows that contain the value 95 in the points column then only the first row with that value will be returned.

Keep this in mind when using the match() function.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Filter a Vector in R
How to Split a Vector into Chunks in R
How to Remove NA Values from Vector in R
How to Remove Specific Elements from Vector in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *