Advertisement
mpettis

stackoverflow:converting-matrix-to-dataframe-works-in-one-ca

Jul 25th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.62 KB | None | 0 0
  1. > ## Solution: reshape is obsolete.  Use reshape2 with acast:
  2. > library(reshape2)
  3. >
  4. > t = 0:3
  5. > thesd = 0.1
  6. > dat = data.frame(
  7. +     a1 = sin(2*pi*t/length(t)) + rnorm(t, sd=thesd),
  8. +     b1 = sin(2*pi*t/length(t) - pi) + rnorm(t, sd=thesd),
  9. +     c1 = sin(2*pi*t/length(t) - pi/2) + rnorm(t, sd=thesd),
  10. +     t  = t
  11. + )
  12. >
  13. > test_mx = acast(melt(dat, id.vars="t"), variable ~ t)
  14. > as.data.frame(test_mx)
  15.              0           1            2           3
  16. a1  0.00881139  1.00898951  0.007116073 -0.82006264
  17. b1 -0.12102260 -0.97846639 -0.095028245  0.84406265
  18. c1 -0.89275912 -0.02657619  1.214589078  0.07990604
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement