Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Is there a function in R that will let me convert a dataset into "long format" but also merge columns?

$
0
0

I have a dataset derived from Pokemon statistics containing a lot of the numerical and categorical data. My end goal is to create a model or recommendation system that a user can input a list of Pokemon and the model finds similar Pokemon they may like. Currently the dataset looks something like this:

ID   Name    Type1    Type2   HP  ATK   DEF
001  Bulba.. Grass    Poison  45  49    49
ect...

I want to convert this data into the "long format", because that format is friendlier with a lot of other functions in R, but I am having trouble dealing with the Type1/Type2 columns. Is there a way I can merge those two into columns into a single column(like "Type") and then have the data converted into the new format? Something like this:

ID   Name    Type    Stat   Value
 001  Bulba.. Grass  HP     45
 001  Bulba.. Poison HP     45
 001  Bulba.. Grass  ATK    49
 001  Bulba.. Poison ATK    49

I understand for Pokemon of Dual-types it would make a pseudo entry, but I don't see any cleaner way to accomplish this. I also know about using dpylr's gather function but I can only really accomplish the Stat column using this method, not the Type issue.

Can anyone help me figure out how I can accomplish this or know any other more efficient methods?


Viewing all articles
Browse latest Browse all 201839

Trending Articles