2

I would like to convert my panel data from long format to wide format. I know there are a number of other questions which hit on this subject, however, I do not believe any of them have the exact answers I am looking for.

my.df <- data.frame(ID=rep(c("A","B","C"), 3), 
    TIME=rep(1:3, each=3), Price=1:9)
my.df

   ID TIME Price
1   A    1     1
2   B    1     2
3   C    1     3
4   A    2     4
5   B    2     5
6   C    2     6
7   A    3     7
8   B    3     8
9   C    3     9

To

  TIME Price-A Price-B Price-C
1    1       1       2       3
2    2       4       5       6
3    3       7       8       9

Thanks for any help you can provide! Francis

6
  • 1
    I can see that the answer to the other question is the same as the answer to my question. However, I think the other question is posed in such general terms that I found it difficult/impossible to implement the answer while my question takes a very specific and common problem (long panel data) and provides a specific answer which I believe will be of use (wide panel data). In addition, the other question does not ask for the ability to name variables based upon composites of the components such as Price-A, Price-B, Price-C. Mar 21, 2014 at 13:34
  • 2
    I'd recommend learning generally how to tidy data, e.g. vita.had.co.nz/papers/tidy-data.html
    – hadley
    Mar 21, 2014 at 14:34
  • Please everybody who marked my post as duplicate. I do not think that this critique is fair. I looked at [other post](stackoverflow.com/questions/5890584/… ) probably 10 times, trying to make sense of it before finally posting this. I know that this post seems redundant but the other post seems too confusing too me. Reshaping is already a confusing operation. To overgeneralize is as helpful as referring an questioner to read the manual. Respectfully, Francis Mar 26, 2014 at 6:02
  • This wasn't meant as a critique (and people did help you, you got comments and answers) but even with your explanation on why you think that post didn't help we failed to understand the difference with it. And I think you actually confirmed it: the solution you accepted is exactly the same solution that was accepted for the other question.
    – plannapus
    Mar 26, 2014 at 8:48
  • The reason we mark questions as duplicate is not to prevent people from helping you but really just to tidy up the website.
    – plannapus
    Mar 26, 2014 at 8:49

2 Answers 2

4

You can use reshape for this:

reshape(my.df,direction="wide", idvar = "TIME", timevar = "ID")


#    TIME Price.A Price.B Price.C
#     1       1       2       3
#     2       4       5       6
#     3       7       8       9
1
  • It is very strange to me that the ID variable and the time variable are switched. Thanks for this! Mar 26, 2014 at 6:04
2

Or you could use dcast from the reshape2 package:

require(reshape2)
dcast(my.df, TIME~ID, value.var="Price")

For this specific example, reshape is faster than dcast. But if the data.frame gets larger, dcast will be faster.

# your example data
require(microbenchmark)
microbenchmark(
  dcast(my.df, TIME~ID, value.var="Price")
  ,
  reshape(my.df,direction="wide", idvar = "TIME", timevar = "ID")
)
# Unit: milliseconds
#    expr      min       lq   median       uq      max  neval
#   dcast 2.655360 2.690616 2.718508 2.766484 4.396740    100
# reshape 2.156866 2.191007 2.221800 2.279147 3.896462    100

# my sample data
rows <- 10
LETTERS2 <- do.call(paste0, list(rep(LETTERS, 26), rep(LETTERS, each=26)))
LETTERS3 <- do.call(paste0, list(rep(LETTERS, 26), rep(LETTERS2, each=26)))
my.df <- data.frame(ID=rep(LETTERS3[1:rows], rows), 
                    TIME=rep(1:rows, each=rows), Price=1:(rows^2))
microbenchmark(
  dcast(my.df, TIME~ID, value.var="Price")
  ,
  reshape(my.df,direction="wide", idvar = "TIME", timevar = "ID")
)
# Unit: milliseconds
#   expr       min       lq   median       uq      max  neval
#  dcast  2.742831 2.795938 2.841681 2.912416 4.570789    100
# reshape 6.571011 6.667631 6.749746 6.857076 8.891662    100

Not the answer you're looking for? Browse other questions tagged or ask your own question.