重新排列r中列表的行

时间:2017-09-22 06:39:29

标签: r excel

如何重新排列包含4列和96行的列表 输入列表:

Well Well.Position BoxNo Position
1   A1  Box1    A1
3   A3  Box1    A2
5   A5  Box1    A3
7   A7  Box1    A4
9   A9  Box1    A5
11  A11 Box1    A6
13  A13 Box1    A7
15  A15 Box1    A8
17  A17 Box1    A9
19  A19 Box1    A10
21  A21 Box1    A11
23  A23 Box1    A12
49  C1  Box1    B1
51  C3  Box1    B2
53  C5  Box1    B3
55  C7  Box1    B4
57  C9  Box1    B5
59  C11 Box1    B6
61  C13 Box1    B7
63  C15 Box1    B8
65  C17 Box1    B9
67  C19 Box1    B10
69  C21 Box1    B11
71  C23 Box1    B12
97  E1  Box1    C1
99  E3  Box1    C2
101 E5  Box1    C3
103 E7  Box1    C4
105 E9  Box1    C5
107 E11 Box1    C6
109 E13 Box1    C7
111 E15 Box1    C8
113 E17 Box1    C9
115 E19 Box1    C10
117 E21 Box1    C11
119 E23 Box1    C12
145 G1  Box1    D1
147 G3  Box1    D2
149 G5  Box1    D3
151 G7  Box1    D4
153 G9  Box1    D5
155 G11 Box1    D6
157 G13 Box1    D7
159 G15 Box1    D8
161 G17 Box1    D9
163 G19 Box1    D10
165 G21 Box1    D11
167 G23 Box1    D12
193 I1  Box1    E1
195 I3  Box1    E2
197 I5  Box1    E3
199 I7  Box1    E4
201 I9  Box1    E5
203 I11 Box1    E6
205 I13 Box1    E7
207 I15 Box1    E8
209 I17 Box1    E9
211 I19 Box1    E10
213 I21 Box1    E11
215 I23 Box1    E12
241 K1  Box1    F1
243 K3  Box1    F2
245 K5  Box1    F3
247 K7  Box1    F4
249 K9  Box1    F5
251 K11 Box1    F6
253 K13 Box1    F7
255 K15 Box1    F8
257 K17 Box1    F9
259 K19 Box1    F10
261 K21 Box1    F11
263 K23 Box1    F12
289 M1  Box1    G1
291 M3  Box1    G2
293 M5  Box1    G3
295 M7  Box1    G4
297 M9  Box1    G5
299 M11 Box1    G6
301 M13 Box1    G7
303 M15 Box1    G8
305 M17 Box1    G9
307 M19 Box1    G10
309 M21 Box1    G11
311 M23 Box1    G12
337 O1  Box1    H1
339 O3  Box1    H2
341 O5  Box1    H3
343 O7  Box1    H4
345 O9  Box1    H5
347 O11 Box1    H6
349 O13 Box1    H7
351 O15 Box1    H8
353 O17 Box1    H9
355 O19 Box1    H10
357 O21 Box1    H11
359 O23 Box1    H12

输出:跳过12行并取第13行并重新排列r

中的整个列表
Well    Well.Position   BoxNo   Position
1   A1  Box1    A1
49  C1  Box1    B1
97  E1  Box1    C1
145 G1  Box1    D1
193 I1  Box1    E1
241 K1  Box1    F1
289 M1  Box1    G1
337 O1  Box1    H1
3   A3  Box1    A2
51  C3  Box1    B2
99  E3  Box1    C2
147 G3  Box1    D2
195 I3  Box1    E2
243 K3  Box1    F2
291 M3  Box1    G2
5   A5  Box1    A3
339 O3  Box1    H2
53  C5  Box1    B3
101 E5  Box1    C3
149 G5  Box1    D3
197 I5  Box1    E3
245 K5  Box1    F3
293 M5  Box1    G3
341 O5  Box1    H3
7   A7  Box1    A4
55  C7  Box1    B4
103 E7  Box1    C4
151 G7  Box1    D4
199 I7  Box1    E4
247 K7  Box1    F4
295 M7  Box1    G4
343 O7  Box1    H4
9   A9  Box1    A5
57  C9  Box1    B5
105 E9  Box1    C5
153 G9  Box1    D5
201 I9  Box1    E5
249 K9  Box1    F5
297 M9  Box1    G5
345 O9  Box1    H5

等。继续采取行,直到安排96行

1 个答案:

答案 0 :(得分:0)

选项是按组创建序列列,然后order

library(data.table)
setDT(df1)[, S1 := seq_len(.N), as.integer(gl(nrow(df1), 12, 
       nrow(df1)))][order(S1)][, S1 := NULL][]
#     Well Well.Position BoxNo Position
# 1:    1            A1  Box1       A1
# 2:   49            C1  Box1       B1
# 3:   97            E1  Box1       C1
# 4:  145            G1  Box1       D1
# 5:  193            I1  Box1       E1
# 6:  241            K1  Box1       F1
# 7:  289            M1  Box1       G1
# 8:  337            O1  Box1       H1
# 9:    3            A3  Box1       A2
#10:   51            C3  Box1       B2
#11:   99            E3  Box1       C2
#12:  147            G3  Box1       D2
#13:  195            I3  Box1       E2
#14:  243            K3  Box1       F2
#15:  291            M3  Box1       G2
#16:  339            O3  Box1       H2
#17:    5            A5  Box1       A3
#18:   53            C5  Box1       B3
#19:  101            E5  Box1       C3
#20:  149            G5  Box1       D3
#21:  197            I5  Box1       E3
#22:  245            K5  Box1       F3
#23:  293            M5  Box1       G3
#24:  341            O5  Box1       H3
#25:    7            A7  Box1       A4
#26:   55            C7  Box1       B4
#27:  103            E7  Box1       C4
#28:  151            G7  Box1       D4
#29:  199            I7  Box1       E4
#30:  247            K7  Box1       F4
#31:  295            M7  Box1       G4
#32:  343            O7  Box1       H4
#33:    9            A9  Box1       A5
#34:   57            C9  Box1       B5
#35:  105            E9  Box1       C5
#36:  153            G9  Box1       D5
#37:  201            I9  Box1       E5
#38:  249            K9  Box1       F5
#39:  297            M9  Box1       G5
#40:  345            O9  Box1       H5
#41:   11           A11  Box1       A6
#42:   59           C11  Box1       B6
#43:  107           E11  Box1       C6
#44:  155           G11  Box1       D6
#45:  203           I11  Box1       E6
#46:  251           K11  Box1       F6
#47:  299           M11  Box1       G6
#48:  347           O11  Box1       H6
#49:   13           A13  Box1       A7
#50:   61           C13  Box1       B7
#51:  109           E13  Box1       C7
#52:  157           G13  Box1       D7
#53:  205           I13  Box1       E7
#54:  253           K13  Box1       F7
#55:  301           M13  Box1       G7
#56:  349           O13  Box1       H7
#57:   15           A15  Box1       A8
#58:   63           C15  Box1       B8
#59:  111           E15  Box1       C8
#60:  159           G15  Box1       D8
#61:  207           I15  Box1       E8
#62:  255           K15  Box1       F8
#63:  303           M15  Box1       G8
#64:  351           O15  Box1       H8
#65:   17           A17  Box1       A9
#66:   65           C17  Box1       B9
#67:  113           E17  Box1       C9
#68:  161           G17  Box1       D9
#69:  209           I17  Box1       E9
#70:  257           K17  Box1       F9
#71:  305           M17  Box1       G9
#72:  353           O17  Box1       H9
#73:   19           A19  Box1      A10
#74:   67           C19  Box1      B10
#75:  115           E19  Box1      C10
#76:  163           G19  Box1      D10
#77:  211           I19  Box1      E10
#78:  259           K19  Box1      F10
#79:  307           M19  Box1      G10
#80:  355           O19  Box1      H10
#81:   21           A21  Box1      A11
#82:   69           C21  Box1      B11
#83:  117           E21  Box1      C11
#84:  165           G21  Box1      D11
#85:  213           I21  Box1      E11
#86:  261           K21  Box1      F11
#87:  309           M21  Box1      G11
#88:  357           O21  Box1      H11
#89:   23           A23  Box1      A12
#90:   71           C23  Box1      B12
#91:  119           E23  Box1      C12
#92:  167           G23  Box1      D12
#93:  215           I23  Box1      E12
#94:  263           K23  Box1      F12
#95:  311           M23  Box1      G12

或者我们可以使用base R

df1[ with(df1, ave(seq_len(nrow(df1)), as.integer(gl(nrow(df1),
           12, nrow(df1))), FUN = seq_along)),]

注意:两种解决方案都提供了OP的帖子中显示的预期输出。