0

I have a dataset from a follow up study with before and after values for various tests (test "A", "B", and so on; up to 20 tests performed). The data frame is arranged so that "pre" and “post” are separated as in TABLE 1:

Group| A_pre  |   B_pre  |  A_post |  B_post 

0       20          20        21        23
1       30          19        10        11
2       10          30        53        34
1       22          25        32        20
2       34          32        40        30
0       30          NA        50        40
0       39          19        40        20
1       40          20        NA        20
2       50          20        10        10
0       34          30        23        10

I would like the data for each test (A and B) to be reordered so that "pre" and "post" are adjacent as below TABLE 2:

Group| A_pre  |    A_post |  B_pre |  B_post 

0       20          21        20        23
1       30          10        19        11
2       10          53        30        34
1       22          32        25        20
2       34          40        32        30
0       30          50        NA        40
0       39          40        19        20
1       40          NA        20        20
2       50          10        20        10
0       34          23        30        10

I have tried using match and rbind but I haven't been successful. I am hoping to avoid using excel and VLOOKup, as they are error prone.

amir.fathi
  • 97
  • 1
  • 13
  • 1
    One "question" per SO question please. – OldProgrammer May 07 '23 at 21:00
  • I didn't think the question would make sense without both parts. I need part A to move on to part B. If you suggest splitting them, I can do that. – amir.fathi May 07 '23 at 21:34
  • Always tag your questions with the underlying programming language. That will ensure they are seen by the maximum number of users. Use the `Edit tags` link under your question to update. – Nick May 07 '23 at 23:45
  • Check out this question. https://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-a-data-frame – Jamie May 08 '23 at 22:44
  • Does this answer your question? [How does one reorder columns in a data frame?](https://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-a-data-frame) – Jamie May 08 '23 at 22:45
  • This looks very promising. Thank you, Jamie. – amir.fathi May 08 '23 at 22:50

0 Answers0