How to move a column from other position to first position in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

A column’s position in an R data frame is crucial specially when we are dealing with a large data set. As the first column appears first, it becomes necessary that we should have columns of the data frame in an order that helps us to look at the important columns easily. For this purpose, we might want to change the position of columns. To change the position of a column to first position we can use single square brackets.

Example

Consider the below data frame −

> set.seed(99)
> x1<-rnorm(20)
> x2<-rpois(20,10)
> x3<-rpois(20,5)
> x4<-runif(20,1,5)
> x5<-sample(1:5,20,replace=TRUE)
> x6<-LETTERS[1:20]
> df<-data.frame(x1,x2,x3,x4,x5,x6)
> df
x1 x2 x3 x4 x5 x6
1 0.2139625022 13 3 3.750865 3 A
2 0.4796581346 12 2 2.381080 2 B
3 0.0878287050 9 4 3.360354 3 C
4 0.4438585075 8 2 1.559041 3 D
5 -0.3628379205 10 7 1.631011 3 E
6 0.1226740295 11 2 2.877371 1 F
7 -0.8638451881 12 7 4.636257 3 G
8 0.4896242667 8 3 2.198420 4 H
9 -0.3641169125 5 5 4.987447 2 I
10 -1.2942420067 9 3 3.608834 2 J
11 -0.7457690454 8 3 2.349861 1 K
12 0.9215503620 11 9 1.992435 2 L
13 0.7500543504 13 5 1.754099 5 M
14 -2.5085540159 14 4 1.908080 2 N
15 -3.0409340953 13 1 2.406410 1 O
16 0.0002658005 11 2 2.741166 1 P
17 -0.3940189942 9 7 4.956848 4 Q
18 -1.7450276608 16 6 2.818134 5 R
19 0.4986314508 10 2 3.686916 2 S
20 0.2709537888 4 6 2.313132 4 T

Suppose we want to change the position of column 6 (x6) to position 1, then it can be done as shown below −

> df<-df[,c(6,1,2,3,4,5)]
> df
x6 x1 x2 x3 x4 x5
1 A 0.2139625022 13 3 3.750865 3
2 B 0.4796581346 12 2 2.381080 2
3 C 0.0878287050 9 4 3.360354 3
4 D 0.4438585075 8 2 1.559041 3
5 E -0.3628379205 10 7 1.631011 3
6 F 0.1226740295 11 2 2.877371 1
7 G -0.8638451881 12 7 4.636257 3
8 H 0.4896242667 8 3 2.198420 4
9 I -0.3641169125 5 5 4.987447 2
10 J -1.2942420067 9 3 3.608834 2
11 K -0.7457690454 8 3 2.349861 1
12 L 0.9215503620 11 9 1.992435 2
13 M 0.7500543504 13 5 1.754099 5
14 N -2.5085540159 14 4 1.908080 2
15 O -3.0409340953 13 1 2.406410 1
16 P 0.0002658005 11 2 2.741166 1
17 Q -0.3940189942 9 7 4.956848 4
18 R -1.7450276608 16 6 2.818134 5
19 S 0.4986314508 10 2 3.686916 2
20 T 0.2709537888 4 6 2.313132 4

In the same way, we can make more changes in the positions as shown below −

> df<-df[,c(6,2,3,5,1,4)]
> df
x5 x1 x2 x4 x6 x3
1 3 0.2139625022 13 3.750865 A 3
2 2 0.4796581346 12 2.381080 B 2
3 3 0.0878287050 9 3.360354 C 4
4 3 0.4438585075 8 1.559041 D 2
5 3 -0.3628379205 10 1.631011 E 7
6 1 0.1226740295 11 2.877371 F 2
7 3 -0.8638451881 12 4.636257 G 7
8 4 0.4896242667 8 2.198420 H 3
9 2 -0.3641169125 5 4.987447 I 5
10 2 -1.2942420067 9 3.608834 J 3
11 1 -0.7457690454 8 2.349861 K 3
12 2 0.9215503620 11 1.992435 L 9
13 5 0.7500543504 13 1.754099 M 5
14 2 -2.5085540159 14 1.908080 N 4
15 1 -3.0409340953 13 2.406410 O 1
16 1 0.0002658005 11 2.741166 P 2
17 4 -0.3940189942 9 4.956848 Q 7
18 5 -1.7450276608 16 2.818134 R 6
19 2 0.4986314508 10 3.686916 S 2
20 4 0.2709537888 4 2.313132 T 6
raja
Published on 11-Aug-2020 14:30:36
Advertisements