Select Rows by Index in R with Examples

  • Post author:

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

r select rows index

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

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