You are currently viewing Select Rows by Name in R

How do I select rows by name in R Programming? In this article, I will explain how to select a single row by name and select multiple rows by name from R DataFrame where row.names match any element from a character vector.
By default, row names in an R data frame are assigned as incremental sequence numbers when the data frame is created. R allows you to assign custom row names either during the creation of the DataFrame or to an existing DataFrame using the rownames() function. To set the column names use colnames() function. For more examples of selecting rows refer to Select Rows from R DataFrame.

1. Quick Examples of Select Rows by Name

Following are quick examples of how to select rows by name in R DataFrame.


# Quick Examples
# Select Row by Row Name
df['1',]

# Select Rows by list of names
df[c('3','6'),]

# Select Row by Custom Row Name
df['r3',]

# Select Rows by list of names
df[c('r3','r6'),]

Let’s create an R DataFrame and assign row names using row.names argument.


# Create DataFrame
df <- data.frame(
  id = c(10,11,12,13,14,15,16,17),
  name = c('sai','ram','deepika','sahithi','kumar','scott','Don','Lin'),
  gender = c('M','M',NA,'F','M','M','M','F'),
  dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16',
                  '1995-03-02','1991-6-21','1986-3-24','1990-8-26')),
  state = c('CA','NY',NA,NA,'DC','DW','AZ','PH'),
  row.names=c('r1','r2','r3','r4','r5','r6','r7','r8')
)
df

Yields below output.


# Output
   id    name gender        dob state
r1 10     sai      M 1990-10-02    CA
r2 11     ram      M 1981-03-24    NY
r3 12 deepika   <NA> 1987-06-14  <NA>
r4 13 sahithi      F 1985-08-16  <NA>
r5 14   kumar      M 1995-03-02    DC
r6 15   scott      M 1991-06-21    DW
r7 16     Don      M 1986-03-24    AZ
r8 17     Lin      F 1990-08-26    PH

2. Select Rows by Name

By using df[rows,columns] approach lets select the rows by row name from the R data frame. To select the rows specify the rows option. As you see above, our R data frame contains custom rows names r1, r2, r3 and so on. The following example selects a single row by name 'r3' from the data frame.

If you have the default row names with the sequential number, you can still use the same approach to select rows by names.


# Select Row by Row Name
df['r3',]

Yields below output.


# Output
   id    name gender        dob state
r3 12 deepika   <NA> 1987-06-14  <NA>

3. Select Rows by List of Row Names

Similarly, you can also select multiple rows by row name by passing the names in a list or vector. You can create a vector using c(). In the following example, I am selecting rows with names r3 and r6.


# Select Rows by list of names
df[c('r3','r6'),]

Yields below output.


# Output
   id    name gender        dob state
r3 12 deepika   <NA> 1987-06-14  <NA>
r6 15   scott      M 1991-06-21    DW

By using the same approach you can also select rows by a condition in R.

4. Conclusion

In this quick article, you have learned how to select a single row by name and multiple rows by row names from R DataFrame with examples. You can also use a similar approach to select rows by index in R.

References