0

I'm working in R, and trying to restructure a data frame that currently has excess info and duplicate cases. What I want is for each person to

A) have their own row, instead of each person x case to have its own row. So in the example below, it would be B2 F USA Beginner Honda Luxury Yes Audi Sport Yes ... and so on all in one row. The second row would begin with B3.

B) And each new column would have a name, e.g.,

    Car     Car Type    Bought    Car_2    Car Type_2    Bought_2
    Honda   Luxury      Yes       Audi     Sport         Yes

C) And delete the rows with just the descriptive info that stays constant (i.e., Driver, Gender, Country, License).

I have nearly 200 cases to do this for, so I'm thinking I need a solution that involves some coding vs. my traditional method of copy & paste. Any assistance would be appreciated!

Example:

    Driver  Gender  Country     License Car     Car Type    Bought
    B2      F       USA     Beginner    Honda   Luxury      Yes
    B2      F       USA     Beginner    Audi    Sport       Yes
    B2      F       USA     Beginner    BMW     Family      Yes
    B2      F       USA     Beginner    Bugatti Sport       No
    B2      F       USA     Beginner    Buick   Family      No
    B3      M       USA     Advanced    Honda   Family      Yes
    B3      M       USA     Advanced    Audi    Luxury      No
    B3      M       USA     Advanced    BMW     Sport       No
    B3      M       USA     Advanced    Bugatti Family      Yes
    B3      M       USA     Advance     Buick   Luxury      Yes
cvdigr
  • 21
  • 3
  • So just in case the is one costumer with 20 cars but all the others have only one or two cars... you want 18x3 columns with NAs for this poor guys with only two cars??? Are you sure this is a good solution? If yes, have a look at `reshape2::cast`. – drmariod Mar 09 '17 at 14:12
  • Indeed, depending on what you want to do with it afterwards, the new data frame might be less easy to work with that this one. Either reshape2 or tidyr should get the right solution for this (https://www.r-bloggers.com/how-to-reshape-data-in-r-tidyr-vs-reshape2/) – Julien Colomb Mar 09 '17 at 14:20
  • http://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – jogo Mar 09 '17 at 14:22
  • Yeah, think of it like a quiz. When do people respond that they want the car? So for each car condition, we're curious who wanted it. And who didn't. – cvdigr Mar 09 '17 at 17:55

0 Answers0