To 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 the position of the row, and columns are used to select columns. In R rows are called observations and columns are called variables.
1. Quick Examples
Below are quick examples of getting specified 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 data frame,
# 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. Use head() function Get Starting N Rows
Use head()
R base function to select the first N rows from the R data frame. The below example returns the first 3 rows from the input data frame.
# 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 data frame. 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
7. Conclusion
In this article, I have provided R examples of selecting data frame rows by index, multiple Indices, list of Indices values, by position range, etc. 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
- How to select rows by condition in R
- How to select rows based on column value
- How to select rows by Name in R
- How to select columns by Name in R
- How to select columns by Index in R
- R subset() function
- R filter() function from dplyr package
- R select() function from dplyr package
- R mutate() function from dplyr package