2
$\begingroup$

What is the technical name for the following data wrangling process? I want to collapse Table A into Table B. (To make the data suitable for ANOVA.)

Table A:

ArmyVet_ID  Served_WW2  Served_KoreanWar    Served_VietnamWar
110001          1              0                    0
110002          1              0                    0
110004          0              1                    0
110005          0              1                    0
110009          0              0                    1
110010          0              0                    1

Table B:

ArmyVet_ID    Served
110001          WW2
110002          WW2
110004          KoreanWar
110005          KoreanWar
110009          VietnamWar
110010          VietnamWar    

Also, the question of how to do the above conversion using R has been asked to death on SO. However, there seem to be way too many ways to do it. If anyone's figured out the absolutely best way to do it (quickest, easiest), I'd appreciate pointers.

Update after correct answer marked below: It turns out that Table A is called "wide format" and B is called "long format".

$\endgroup$
3
  • 2
    $\begingroup$ The answer given is right, but 'wide format to long format' might be even more specific, also, I recommend reshape2 instead of reshape, reshape is, as I understand it, underdocumented $\endgroup$
    – Shape
    Commented Jun 8, 2016 at 22:42
  • 1
    $\begingroup$ @Shape Do you know of any more synonyms of "reshape"? It shocks me that when I look for 'reshape' and Weka or Rapidminer I get nothing on google. $\endgroup$ Commented Jun 9, 2016 at 6:08
  • 1
    $\begingroup$ It's "normalization" in database theory. $\endgroup$
    – Diego
    Commented Jun 9, 2016 at 8:55

2 Answers 2

2
$\begingroup$

It is usually called reshaping! For a great description of the process, see this walkthrough, or read up on Hadley Wickham's documentation for the reshape package!

$\endgroup$
1
$\begingroup$
df['Served'] = (df.iloc[:, 1:] == 1).idxmax(1)
$\endgroup$
1
  • $\begingroup$ Welcome to the site! I have submitted an edit for your answer so that it displays as properly formatted code with the markdown language. $\endgroup$ Commented Mar 15, 2019 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.