Question

I'm trying to reshape this ranking data into something more graphical.. perhaps something like: ggplot(party2, aes(x=Preference, y=Ranking, colour=id)+geom_line(). First I have to reshape it though.

Here's the data so far:

> head(party)
  Theme Music/DJ Drink deals People Location
3     3        4           5      1        2
4     2        3           5      1        4
5     5        4           3      1        2
6     4        1           5      2        3

The goal is for the data to look like this:

id Preference     Ranking
1    Theme           3
1    Music/DJ        4
1    Drink deals     5
1    People          1
1    Location        2
2    Theme           2
2    Music/DJ        3
2    Drink deals     5

To reshape the data, I used Hadley's code from this link: How to reshape this dataframe with the reshape package, but I'm still having trouble. I think I'm close.

My code so far is:

party.pref<-c("Theme", "Music/DJ", "Drink deals", "People", "Location")
party<-data[,party.pref]
party<-na.omit(party)
party2<-cbind(party, id=seq(1,nrow(party),1)) # Add IDs column
gp<-melt(party2, id="id", measured=party.pref)
dcast(gp, ... ~party.pref)

And it's coming out like this:

  id    variable   Drink deals Location Music/DJ People Theme
  1       Theme        <NA>     <NA>     <NA>   <NA>     3
  1    Music/DJ        <NA>     <NA>     <NA>   <NA>     4
  1 Drink deals        <NA>     <NA>     <NA>   <NA>     5
  1      People        <NA>     <NA>     <NA>   <NA>     1
  1    Location        <NA>     <NA>     <NA>   <NA>     2
  2       Theme        <NA>     <NA>        2   <NA>  <NA>

So as you can see, if all those factored columns just became "Ranking" and I got rid of all the NAs, I'd have my answer, but I'm not sure how to do that. I think I'm doing something wrong on the "dcast" or the "melt", but I'm not sure which.

Any help is greatly appreciated,thanks!

Was it helpful?

Solution

You need to use melt, not dcast. dcast is for going from long to wide format, you are trying to do the opposite.

party <- cbind(id=1:nrow(party), party) # add id
melt(party, id.vars="id")               # melt, indicate "id" should be a column in result     

this produces:

#     id variable value
#  1   1    Theme     3
#  2   2    Theme     4
#  3   3    Theme     5
#  4   4    Theme     6
#  5   1 Music.DJ     3
#  6   2 Music.DJ     2
# ...
# 20  4   People     2
# 21  1 Location     2
# 22  2 Location     4
# 23  3 Location     2
# 24  4 Location     3

OTHER TIPS

Alex, just adding one more info.

If the lines mean something and you do not want to lose information, you should add one more column naming them. Then you melt and recast.

party <- read.table(text=
"Theme Music/DJ Drink/deals People Location
     3        4           5      1        2
     2        3           5      1        4
     5        4           3      1        2
     4        1           5      2        3", header=TRUE)

### Add one more column with the meaning of each line:
party$ranking <- c("ranking1", "ranking2", "ranking3", "ranking4")

party

#This wil give you:
     Theme Music.DJ Drink.deals People Location  ranking
1     3        4           5      1        2 ranking1
2     2        3           5      1        4 ranking2
3     5        4           3      1        2 ranking3
4     4        1           5      2        3 ranking4

#then melt and dcast
library(reshape2)
ranking <- melt(party)
ranking <- dcast(ranking, variable~ranking)
ranking

#this will give you
    variable ranking1 ranking2 ranking3 ranking4
1       Theme        3        2        5        4
2    Music.DJ        4        3        4        1
3 Drink.deals        5        5        3        5
4      People        1        1        1        2
5    Location        2        4        2        3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top