2

I'd like to turn this:

   game_date  team_id  opponent_id team_away team_outcome   opponent_outcome
1 2016-03-09    a           b     FALSE          loss              win

  structure(list(game_date = "2016-03-09", team_id = "a", opponent_id = "b", 
team_away = FALSE, team_outcome = "loss", opponent_outcome = "win"), .Names = c("game_date", 
"team_id", "opponent_id", "team_away", "team_outcome", "opponent_outcome"
), class = "data.frame", row.names = "1")

Into this:

game_date     team outcome   away
2016-03-09     a     loss   FALSE
2016-03-09     b     win    TRUE

I'm having trouble determining the best way to do this with reshape. I've tried for example

dcast(x, team_id + opponent_id ~ team_outcome)
melt(x, id.vars = c("team_id", "opponent_id"), measure.vars = c("team_outcome", "team_away") 
tcash21
  • 4,880
  • 4
  • 32
  • 39
  • Possible duplicate of [Reshaping data.frame from wide to long format](http://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) – alexwhitworth Mar 11 '16 at 00:54
  • There's some extra logic that needs to happen after the reshape so I think it's different enough. – tcash21 Mar 11 '16 at 05:38

2 Answers2

1

With reshape you could do:

y=melt(x,id=c("game_date","team_id","opponent_id","team_away")
    ,measure.vars=c("team_outcome","opponent_outcome"))

To get:

   game_date team_id opponent_id team_away         variable value
1 2016-03-09       a           b     FALSE     team_outcome  loss
2 2016-03-09       a           b     FALSE opponent_outcome   win

Then these to get your desired columns:

y$team=ifelse(y$variable == "team_outcome","a","b")
y$away=ifelse(y$variable == "team_outcome" & y$team_away == FALSE,"yes","no")
z=y[,c("game_date","team","value","away")]
desc
  • 1,190
  • 1
  • 12
  • 26
  • 1
    Thanks! I figured there'd be an `ifelse` in there somewhere but was probably over-complicating it. – tcash21 Mar 11 '16 at 05:37
0

Found a way to do it in base R but still curious if there's a melt/reshape/dcast way.

all_outcomes<-data.frame(c(x$team_id, x$opponent_id), 
c(x$team_outcome, x$opponent_outcome), 
c(x$team_away, !x$team_away), 
c(x$game_date))
tcash21
  • 4,880
  • 4
  • 32
  • 39
  • You ought to use the capacity of data.frame to accept column names. – IRTFM Mar 10 '16 at 23:19
  • Yea true, I ended up renaming the columns after, but definitely easier and less lines of code to do it in the same statement. – tcash21 Mar 11 '16 at 05:36