You are currently viewing Select Rows by Index in R with Examples

We can select rows (observations) by Index in R by using a single square bracket operator df[rows,columns], From the square bracket, we should be using rows position, and columns are used to select columns. In R rows are called observations and columns are called variables.

1. Quick Examples

The following are quick examples of how to return rows/observations by Index in R.


# Quick Examples

# Select Rows by Index
df[3,]

# Select Rows by List of Index Values
df[c(3,4,6),]

# Select Rows by Index Range
df[3:6,]

# Select first N rows
head(df,3)

# Select last N rows
tail(df,3)

Let’s create an R DataFrame, run these examples and explore the output.


# 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 Index

Use the square bracket operator with df[] notation to select rows by index in R, The syntax of this notation is df[rows, columns], replace rows with the index number, index range, or list of index values.


# Select Rows by Index
df[3,]

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

3. Select Rows by List of Index Values

If you have a list or vector of row indexes, use it with df[] notation to select the rows by a list of index values. The following example selects observations 3, 4 and 6


# Select Rows by List of Index Values
df[c(3,4,6),]

# Output
#   id    name gender        dob state
#r3 12 deepika   <NA> 1987-06-14  <NA>
#r4 13 sahithi      F 1985-08-16  <NA>
#r6 15   scott      M 1991-06-21    DW

4. Select Rows by Index Range

Sometimes you would also be required to select rows by index range in R, this can be easily achieved by specifying the start and end position (start position : end position). The following example selects all rows between positions 3 and 6.


# Select Rows by Index Range
df[3:6,]

# Output
#   id    name gender        dob state
#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

5. Select First N Rows

Use head() R base function to select the first N rows from R DataFrame. The below example returns the first 3 rows from the input DataFrame.


# Select first N rows
head(df,3)

# 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>

6. Select Last N Rows

Use tail() R base function to select the last N rows from R DataFrame. The below example returns the last 3 rows.


# Select last N rows
tail(df,3)

# Output
#   id  name gender        dob state
#r6 15 scott      M 1991-06-21    DW
#r7 16   Don      M 1986-03-24    AZ
#r8 17   Lin      F 1990-08-26    PH

5. Conclusion

In this article, I have explained R examples of how to select DataFrame rows by Index, by multiple Indices, by List of Indices values, by position range e.t.c. Also, learned how to select the first N rows using the head() function and the last N rows using the tail() function. For more similar examples, refer to select rows in R.

Related Articles

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium