Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the nth row from data frame in R

Tags:

r

dataset

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
like image 846
maksadbek Avatar asked Jan 09 '16 14:01

maksadbek


People also ask

How do you find the nth row of a data frame?

To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df. iloc[4] will return the 5th row because row numbers start from 0.

How do you get a specific row in R?

R – Get Specific Row of Matrix To get a specific row of a matrix, specify the row number followed by a comma, in square brackets, after the matrix variable name. This expression returns the required row as a vector.

How do I select part of a data frame in R?

To select a specific column, you can also type in the name of the dataframe, followed by a $ , and then the name of the column you are looking to select. In this example, we will be selecting the payment column of the dataframe.


2 Answers

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,]
like image 163
gung - Reinstate Monica Avatar answered Oct 13 '22 16:10

gung - Reinstate Monica


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

library(dplyr)
slice(dataFrame, 10)

See details here slice: Select rows by position

like image 3
Andrii Avatar answered Oct 13 '22 17:10

Andrii