Here is my data.
df <- data.frame(Aa = 1:10, Ab = 11:20,Ca = 41:50, Cb = 51:60, Ba = 21:30, Bb = 31:40,
Da = 51:60, Db = 61:70) # actual data has > 6000 columns
df
Aa Ab Ca Cb Ba Bb Da Db
1 1 11 41 51 21 31 51 61
2 2 12 42 52 22 32 52 62
3 3 13 43 53 23 33 53 63
4 4 14 44 54 24 34 54 64
5 5 15 45 55 25 35 55 65
6 6 16 46 56 26 36 56 66
7 7 17 47 57 27 37 57 67
8 8 18 48 58 28 38 58 68
9 9 19 49 59 29 39 59 69
10 10 20 50 60 30 40 60 70
mf <- data.frame (SN = 1:5, colname = c("A", "B", "C", "D", "E"))
mf
SN colname
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
I want to order the columns of df (actually pairs with suffix a and b) based on mf$colname. Exclude those column that is not in mf.
So output will look like:
df1 <- data.frame(Aa = 1:10, Ab = 11:20, Ba = 21:30, Bb = 31:40,Ca = 41:50, Cb = 51:60,
Da = 51:60, Db = 61:70)
df1
Aa Ab Ba Bb Ca Cb Da Db
1 1 11 21 31 41 51 51 61
2 2 12 22 32 42 52 52 62
3 3 13 23 33 43 53 53 63
4 4 14 24 34 44 54 54 64
5 5 15 25 35 45 55 55 65
6 6 16 26 36 46 56 56 66
7 7 17 27 37 47 57 57 67
8 8 18 28 38 48 58 58 68
9 9 19 29 39 49 59 59 69
10 10 20 30 40 50 60 60 70
Edits:
order does not mean alphabetic order. order based on mf$colnames
thanks;
Major edits:
I think the example I provided is clear what I want to do, as I my desired column by coincidence alphabetical order. But my intention is not true. Please consider another example.
df <- data.frame (k1a = 1:10, k1b = 11:20, a1a = 21:30, a1b = 31:40,
j1a = 41:50, j1b = 51:60, p1a = 61:70, p1b = 71:80)
df
k1a k1b a1a a1b j1a j1b p1a p1b
1 1 11 21 31 41 51 61 71
2 2 12 22 32 42 52 62 72
3 3 13 23 33 43 53 63 73
4 4 14 24 34 44 54 64 74
5 5 15 25 35 45 55 65 75
6 6 16 26 36 46 56 66 76
7 7 17 27 37 47 57 67 77
8 8 18 28 38 48 58 68 78
9 9 19 29 39 49 59 69 79
10 10 20 30 40 50 60 70 80
mf <- data.frame (SN = 1:5, colname = c("p1", "a1", "j1", "k1", "e1")
mf
SN colname
1 1 p1
2 2 a1
3 3 j1
4 4 k1
5 5 e1
this desired output
df1 <- data.frame ( p1a = 61:70, p1b = 71:80, a1a = 21:30, a1b = 31:40,
j1a = 41:50, j1b = 51:60, k1a = 1:10, k1b = 11:20)
df1
p1a p1b a1a a1b j1a j1b k1a k1b
1 61 71 21 31 41 51 1 11
2 62 72 22 32 42 52 2 12
3 63 73 23 33 43 53 3 13
4 64 74 24 34 44 54 4 14
5 65 75 25 35 45 55 5 15
6 66 76 26 36 46 56 6 16
7 67 77 27 37 47 57 7 17
8 68 78 28 38 48 58 8 18
9 69 79 29 39 49 59 9 19
10 70 80 30 40 50 60 10 20
Changed answer:
To the updated question, I think you can do it as followed. Given dataset:
You can use
grep()to find the indexes of variables names that correspond to each of the elements ofmf$colnaneThis becomes: