maksadbek
maksadbek

Reputation: 1592

How to get the nth row from data frame in R

For example, I have the following data frame:

> dataFrame <- read.csv(file="data.csv")
> dataFrame
    Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
3      12     149 12.6   74     5   3
4      18     313 11.5   62     5   4
5      NA      NA 14.3   56     5   5
6      28      NA 14.9   66     5   6
7      23     299  8.6   65     5   7
8      19      99 13.8   59     5   8
9       8      19 20.1   61     5   9
10     NA     194  8.6   69     5  10

How can I get the nth row ? For example, the 10th

10     NA     194  8.6   69     5  10

Upvotes: 23

Views: 46874

Answers (3)

Shriya Acharya
Shriya Acharya

Reputation: 1

You can get the number of rows using nrow and then find the nth row.

dataFrame[nrow(dataFrame),]

Upvotes: 0

Andrii
Andrii

Reputation: 3043

You can also use "slice" function in the following way:

library(dplyr)
slice(dataFrame, 10)

See details here slice: Select rows by position

Upvotes: 6

gung - Reinstate Monica
gung - Reinstate Monica

Reputation: 11893

You just need to use the square brackets to index your dataframe. A dataframe has two dimensions (rows and columns), so the square brackets will need to contain two pieces of information: row 10, and all columns. You indicate all columns by not putting anything. So your code would be this:

dataFrame[10,]

Upvotes: 19

Related Questions