How to reorder the row indices in an R data frame?



If we have a data frame where row indices are not in correct sequence then we can set them to NULL to get the correct sequence of row numbers starting from 1.

For example, if we have a data frame called df which has row ordered in an inappropriate way then we can reorder the row indices in correct order by using the command given below −

row.names(df)<- NULL

Example 1

Following snippet creates a sample data frame −

x<-rnorm(20)
y<-rnorm(20)
df1<-data.frame(x,y)
df1

The following dataframe is created −

       x           y
1   0.79220702   0.74396588
2  -0.27362051  -0.09125937
3  -1.34248184  -1.42285864
4  -1.10430033   0.26453631
5  -0.52047324  -0.54336532
6  -1.20612600   0.30704459
7   1.62577878   0.58061047
8  -0.09384629  -0.16973222
9   2.83732637  -0.58291919
10 -0.12450580  -0.76230292
11  1.50889524   1.13920262
12  0.19941471   0.81462851
13 0.21843905   -0.49421398
14 -1.97555113  -0.78007517
15 -1.02591803  -0.21299362
16 1.43648115    0.71561607
17 -0.15479259   0.16713844
18  0.06903559  -2.15635528
19  1.51844896   0.49315357
20  0.25861358  -1.43660348

To change the order of row indices in df1, add the following code to the above snippet −

x<-rnorm(20)
y<-rnorm(20)
df1<-data.frame(x,y)
df1<-df1[sample(1:nrow(df1)),]
df1

Output

If you execute all the above given snippets as a single program, it generates the following output −

       x            y
16  1.43648115   0.71561607
6  -1.20612600   0.30704459
3  -1.34248184  -1.42285864
20  0.25861358  -1.43660348
9   2.83732637  -0.58291919
1   0.79220702   0.74396588
8  -0.09384629  -0.16973222
19  1.51844896   0.49315357
2  -0.27362051  -0.09125937
4  -1.10430033   0.26453631
12  0.19941471   0.81462851
13  0.21843905  -0.49421398
18  0.06903559  -2.15635528
10 -0.12450580  -0.76230292
14 -1.97555113  -0.78007517
5  -0.52047324  -0.54336532
11  1.50889524   1.13920262
17 -0.15479259   0.16713844
7   1.62577878   0.58061047
15 -1.02591803  -0.21299362

To reorder the row indices in df1, add the following code to the above snippet −

x<-rnorm(20)
y<-rnorm(20)
df1<-data.frame(x,y)
df1<-df1[sample(1:nrow(df1)),]
row.names(df1)<-NULL
df1

Output

If you execute all the above given snippets as a single program, it generates the following output −

        x          y
1   1.43648115   0.71561607
2  -1.20612600   0.30704459
3  -1.34248184  -1.42285864
4   0.25861358  -1.43660348
5   2.83732637  -0.58291919
6   0.79220702   0.74396588
7  -0.09384629  -0.16973222
8   1.51844896   0.49315357
9  -0.27362051  -0.09125937
10 -1.10430033   0.26453631
11  0.19941471   0.81462851
12  0.21843905  -0.49421398
13  0.06903559  -2.15635528
14 -0.12450580  -0.76230292
15 -1.97555113  -0.78007517
16 -0.52047324  -0.54336532
17  1.50889524   1.13920262
18 -0.15479259   0.16713844
19  1.62577878   0.58061047
20 -1.02591803  -0.21299362

Example 2

Following snippet creates a sample data frame −

a<-rpois(20,2)
b<-rpois(20,5)
c<-rpois(20,2)
df2<-data.frame(a,b,c)
df2

The following dataframe is created −

   a  b  c
1  2  1  2
2  0 10  0
3  1  2  1
4  2  5  0
5  4  4  2
6  2  4  5
7  3  5  2
8  4  2  3
9  4  6  2
10 1  3  3
11 1  9  1
12 2  4  3
13 4  6  1
14 2  4  1
15 4  7  3
16 1  2  0
17 0  5  1
18 2  4  1
19 0  8  5
20 3  6  3

To change the order of row indices in df2, add the following code to the above snippet −

a<-rpois(20,2)
b<-rpois(20,5)
c<-rpois(20,2)
df2<-data.frame(a,b,c)
df2<-df2[sample(1:nrow(df2)),]
df2

Output

If you execute all the above given snippets as a single program, it generates the following output −

   a  b  c
6  2  4  5
7  3  5  2
9  4  6  2
15 4  7  3
2  0 10  0
19 0  8  5
10 1  3  3
20 3  6  3
16 1  2  0
3  1  2  1
18 2  4  1
5  4  4  2
12 2  4  3
17 0  5  1
4  2  5  0
8  4  2  3
11 1  9  1
13 4  6  1
1  2  1  2
14 2  4  1

To reorder the row indices in df2, add the following code to the above snippet −

a<-rpois(20,2)
b<-rpois(20,5)
c<-rpois(20,2)
df2<-data.frame(a,b,c)
df2<-df2[sample(1:nrow(df2)),]
row.names(df2)<-NULL
df2

Output

If you execute all the above given snippets as a single program, it generates the following output −

   a  b  c
1  2  4  5
2  3  5  2
3  4  6  2
4  4  7  3
5  0 10  0
6  0  8  5
7  1  3  3
8  3  6  3
9  1  2  0
10 1  2  1
11 2  4  1
12 4  4  2
13 2  4  3
14 0  5  1
15 2  5  0
16 4  2  3
17 1  9  1
18 4  6  1
19 2  1  2
20 2  4  1

Advertisements