How to subset nth row from an R data frame?


We can find subsets using many ways in R and the easiest way is to use single-square brackets. If we want to subset a row or a number of consecutive or non-consecutive rows then it can be directly done with the data frame name and the single-square brackets. For example, if we have a data frame called df and we want to subset 1st row of df then we can use df[1,] and that’s it.

Example

Consider the below data frame:

Live Demo

> set.seed(214)
> x<-rnorm(20)
> y<-rnorm(20,1,0.5)
> z<-rnorm(20,2,0.57)
> a<-rnorm(20,1,0.27)
> b<-rpois(20,2)
> c<-rpois(20,8)
> q<-rpois(20,5)
> w<-rpois(20,1)
> df1<-data.frame(x,y,z,a,b,c,q,w)
> df1

Output

x y z a b c q w
1 -0.46774980 1.1546101 2.3342540 0.9143609 2 8 6 0
2 0.04088223 0.7590773 2.2095770 0.9712316 1 5 6 2
3 1.00335193 1.7272210 1.7318417 1.1871876 2 8 7 0
4 2.02522505 0.8515016 1.9366870 0.4658958 4 4 3 2
5 0.30640096 1.2055142 2.5719530 0.8469379 4 9 5 1
6 0.42577748 1.6967249 1.5668833 0.9602888 5 7 5 1
7 0.74889267 2.0073967 2.4715450 0.7116510 3 5 5 1
8 0.44645148 1.0209466 1.1198797 1.3250236 2 10 6 1
9 -2.20514180 1.6927716 2.1447475 1.1950635 0 7 5 2
10 1.98181366 1.3930763 2.3038074 1.1096453 2 6 5 1
11 -2.62555247 1.2849028 1.7522339 1.1864803 2 5 2 1
12 -0.72301789 1.0450742 0.2930952 1.1930435 2 8 4 2
13 -0.88306915 0.8239228 2.5604929 0.9686630 1 8 2 0
14 -0.52517037 1.3413851 2.0189895 0.8643248 2 9 4 0
15 -0.94756990 0.2507953 1.1719018 1.0294649 5 10 3 1
16 -0.51916173 1.1889573 2.1277015 0.6870978 3 8 5 0
17 -1.12071138 1.4807661 1.9248328 1.0950342 1 12 4 2
18 0.42359496 1.5472942 1.7000941 0.8440301 1 12 2 2
19 0.46975875 1.4835207 1.2282101 1.0651645 2 6 7 0
20 -0.21269994 0.8056228 1.7694949 0.9686047 5 11 6 0

Subsetting different rows of df1:

Example

> df1[1,]

Output

x y z a b c q w
1 -0.4677498 1.15461 2.334254 0.9143609 2 8 6 0

Example

> df1[2,]

Output

x y z a b c q w
2 0.04088223 0.7590773 2.209577 0.9712316 1 5 6 2

Example

> df1[3,]

Output

x y z a b c q w
3 1.003352 1.727221 1.731842 1.187188 2 8 7 0

Example

> df1[5,]

Output

x y z a b c q w
5 0.306401 1.205514 2.571953 0.8469379 4 9 5 1

Example

> df1[8,]

Output

x y z a b c q w
8 0.4464515 1.020947 1.11988 1.325024 2 10 6 1

Example

> df1[9,]

Output

x y z a b c q w
9 -2.205142 1.692772 2.144747 1.195064 0 7 5 2

Example

> df1[12,]

Output

x y z a b c q w
12 -0.7230179 1.045074 0.2930952 1.193043 2 8 4 2

Example

> df1[15,]

Output

x y z a b c q w
15 -0.9475699 0.2507953 1.171902 1.029465 5 10 3 1

Example

> df1[18,]

Output

x y z a b c q w
18 0.423595 1.547294 1.700094 0.8440301 1 12 2 2

Example

> df1[20,]

Output

x y z a b c q w
20 -0.2126999 0.8056228 1.769495 0.9686047 5 11 6 0

Let’s have a look at another example:

Example

Live Demo

> v1<-rexp(20,1.24)
> v2<-rexp(20,3.7)
> v3<-runif(20,2,8)
> df2<-data.frame(v1,v2,v3)
> df2

Output

       v1         v2       v3
1 0.20602134 0.06916392 6.107286
2 0.29568560 0.36353986 3.529261
3 0.12250478 0.18168857 4.524547
4 2.37228009 0.20580564 6.795179
5 0.51194665 0.03005732 3.208580
6 0.25267457 0.12722097 2.184198
7 0.03742423 0.01711751 4.135536
8 0.45572624 0.29921997 6.046839
9 0.63617201 0.55386034 7.812157
10 0.81699828 0.56160708 4.071993
11 0.26570318 0.06759301 3.625271
12 0.63101790 0.10742853 2.573730
13 0.60664724 0.28611242 4.053965
14 0.79000859 0.09818221 6.257031
15 0.44555943 0.01828257 3.953676
16 1.87292479 0.20373389 3.407394
17 0.17258681 0.20278572 5.874761
18 0.09658603 0.09844967 5.382432
19 0.04970458 0.46433382 7.007515
20 0.31233081 0.06999427 4.855714

Subsetting different rows of df2:

Example

> df2[3,]

Output

v1 v2 v3
3 0.1225048 0.1816886 4.524547

Example

> df2[5,]

Output

v1 v2 v3
5 0.5119466 0.03005732 3.20858

Example

> df2[7,]

Output

v1 v2 v3
7 0.03742423 0.01711751 4.135536

Example

> df2[9,]

Output

v1 v2 v3
9 0.636172 0.5538603 7.812157

Example

> df2[10,]

Output

v1 v2 v3
10 0.8169983 0.5616071 4.071993

Example

> df2[12,]

Output

v1 v2 v3
12 0.6310179 0.1074285 2.57373

Example

> df2[15,]

Output

v1 v2 v3
15 0.4455594 0.01828257 3.953676

Example

> df2[17,]

Output

v1 v2 v3
17 0.1725868 0.2027857 5.874761

Example

> df2[20,]

Output

v1 v2 v3
20 0.3123308 0.06999427 4.855714

Updated on: 21-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements