This question already has an answer here:
I have a dataframe that I pulled in from an API. After some cleaning it looks something like this:
Title Year Rating Title Year Rating Title Year Rating
Movie 1 1997 6.7 Movie 2 1987 8.2 Movie 3 2009 7.1
The column headers repeat, and in this case a single row contains 3 separate entries.
How would I reshape this so that I end up with 3 columns (Title, Year, Rating) and 3 rows (Movie 1, Movie 2, Movie 3)?
What's the simplest way of doing this?
I think if you got that data from the API your cleaning must have gone wrong somewhere. You've lost all information to figure out which rating and which title go with which movie besides column order.
But anyway, you can do it like this:
Convert the input data.frame to a list and split the columns into groups according to their common column names. Then unlist each group of columns to produce a single column in each group and convert back to a data.frame. (This also works if there is more than one row in
DF
.)giving:
Note: We assumed this input:
This can be done with
melt
fromdata.table
which can take multiple columns in themeasure
by specifying thepattern
data