I have a dataset with multiple tables in the same Excel sheet that I'd like to combine. The sheet's data is formatted like this:
group_no group variable marker1 marker2 marker3
1 GR-01-1 1 - - A
1 GR-01-1 2 B A B
1 GR-01-1 3 A B -
1 GR-01-1 4 - - -
1 GR-01-1 5 B - B
group_no. group variable marker 4 marker5 marker6 marker7
2 GR-01-1a 1 B - A -
2 GR-01-1a 2 B A - B
2 GR-01-1a 3 A B - B
2 GR-01-1a 4 A A A A
group_no group variable marker8
3 GR-01-2 1 B
3 GR-01-2 2 A
3 GR-01-2 3 -
group_no group variable marker9 marker10
4 GR-02-1 1 B A
4 GR-02-1 2 A A
4 GR-02-1 3 - B
4 GR-02-1 4 B B
4 GR-02-1 5 A B
4 GR-02-1 6 - A
group_no group variable marker11 marker12 marker13
5 GR-02-2 d B B A
5 GR-02-2 e A B B
5 GR-02-2 f B - -
5 GR-02-2 g - B -
As can be seen above, the supplier of the data did not separate out the names of the markers into unique columns. All the tables are stacked on top of one another. What I want to do is transform the data into this:
marker_name group_no group 1 2 3 4 5 6 a b c d e f g
marker1 GR-01-1 1 - B A - B
marker2 GR-01-1 1 - A B - -
marker3 GR-01-1 1 A B - - B
marker4 GR-01-1a 2 B B A A
marker5 GR-01-1a 2 - A B A
marker6 GR-01-1a 2 A - - A
marker7 GR-01-1a 2 - B B A
marker8 GR-01-2 3 B A -
marker9 GR-02-1 4 B A - B A -
marker10 GR-02-1 4 A A B B B A
marker11 GR-02-2 5 B A B -
marker12 GR-02-2 5 B B - B
marker13 GR-02-2 5 A B - -
Is there a way to do this in R? A loop maybe? I started off with transposing in R, but that didn't work and I wonder if there may have been a way to read the data in by group_no. But no luck. I am still a novice, I guess. There are 917 tables in my data set in the single sheet. I'm hoping I don't have to assign each to a new R object one-by-one and then rbind or cbind.