Search code examples
rdataframereshapechunksdata-management

R reshape, restructure dataframe by chunks


I am trying to reshape a dataframe:

Currently it looks like this:

ID   | Gender |A1 | A2 | A3 | B1 | B2 | B3
ID_1 | m      | 3 | 3  | 3  | 2  | 3  | 2 
ID_2 | f      | 1 | 1  | 1  | 4  | 4  | 4

I want to have something like:

 ID   | Gender | A1 | A2 | A3
 ID_1 | m      | 3  | 3  |  3   <- this would be columns A1 - A3 for ID 1
 ID_1 | m      | 2  | 2  |  2   <- this would be columns B1 - B3 for ID 1
 ID_2 | f      | 1  | 1  |  1   <- this would be columns A1 - A3 for ID 2
 ID_2 | f      | 4  | 4  |  4   <- this would be columns B1 - B3 for ID 2

(A1 and B1 / A2 and B2 are the same variables (with regard to the content), so for example: A1 and B1 would be both variables for the result of Test 1 and A2 and B2 both contain the result of Test 2. So in order to evaluate it I need all the result of Test1 in one column and all of Test 2 in another column. I tried to solve this with "melt", but it only melts down the dataframe one by one, not as chunks. (since I need to keep the first 2 columns the way they are and only rearrange the last 4 columns, but as chunks of three) Any other ideas? Thanks!


Solution

  • One liner using reshape from base R.

    reshape(dat, varying = 3:8, idvar = 1:2, direction = 'long', drop=FALSE, 
       timevar = 'Test')
    
               ID Gender Test Test1 Test2 Test3
    ID_1.m.A ID_1      m    A    A1    A2    A3
    ID_2.f.A ID_2      f    A    A1    A2    A3
    ID_1.m.B ID_1      m    B    B1    B2    B3
    ID_2.f.B ID_2      f    B    B1    B2    B3