19

I have data that comes out of a DB in a normalized way with a field for year, state, and value.

I would like to do analysis on the data and need it formatted where each year is a field and not a record.So I would like the data where each record is a state and then there's a field for each year and each value for those fields are the value for that year and that state.

Is there a command for doing this?

So I have:

State  Year  Value  
   KY  1998     56  
   KY  1997     78  
   IL  1998     48  
   IL  1997     72

and I want:

State  1997_value  1998_value  
   KY          78          56  
   IL          72          48
zx8754
  • 52,746
  • 12
  • 114
  • 209
JD Long
  • 59,675
  • 58
  • 202
  • 294

3 Answers3

22

You want to use the reshape() function.

reshape(data, idvar="State", timevar="Year", direction="wide")
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
Josh Reich
  • 6,477
  • 5
  • 28
  • 26
6

Another option is to use the reshape package, created by the inimitable Hadley Wickham:

library(reshape)

tuna<-melt(data,id.vars=c("State","Year"))

cast(tuna,State~Year~variable)
Matt Parker
  • 26,709
  • 7
  • 54
  • 72
2

You can even combine the melt and cast lines into one call to the recast function.

ds <- data.frame(State = c("KY", "KY", "IL", "IL"), 
Year = c(1998, 1997, 1998, 1997), 
Value = c(56, 78, 48, 72))

library(reshape)
recast(ds, State ~ Year, id.var = c("State", "Year"))
Thierry
  • 18,049
  • 5
  • 48
  • 66