Consider the following pair of lists
ID<-c("A", "B")
Var2<-c("T_X", "X_F", "R_X", "T_Y", "Y_F", "R_Y")
I have created the following dataframe
df2<-expand.grid(ID, Var2)
The resultant dataframe is as follows
Var1 Var2
1 A T_X
2 B T_X
3 A X_F
4 B X_F
5 A R_X
6 B R_X
7 A T_Y
8 B T_Y
9 A Y_F
10 B Y_F
11 A R_Y
12 B R_Y
I would like to reorder the dataframe by Var1 column so that all values corresponding to A are together and likewise with B. (Note this is a toy dataset and the real number of unique values in Var1 can be much higher).
The following is the expected output
Var1 Var2
1 A T_X
3 A X_F
5 A R_X
7 A T_Y
9 A Y_F
11 A R_Y
2 B T_X
4 B X_F
6 B R_X
8 B T_Y
10 B Y_F
12 B R_Y
I have tried df2%>% group_by(Var1). this has left the dataframe unchanged.
I request someone to help me here.
df2 %>% arrange(Var1)will do the job right?