[R] Can not melt data.frame
Rui Barradas
ruipbarradas at sapo.pt
Fri Feb 8 20:12:59 CET 2013
Hello,
The id.vars must "be integer (variable position) or string (variable
name)" (from the help page ?melt.data.frame)
This seems to work.
myd <- mydata
myd$date <- as.character(myd$date)
melt(myd, id.vars = c("date"))
Hope this helps,
Rui Barradas
Em 08-02-2013 18:31, John Kane escreveu:
> I realize it's -12C and we're having the next best thing to a blizzard but why can I not melt this data frame.
>
> I am missing something terribly obvious but I just don't understand what the error message is saying.
>
> John Kane
> Kingston ON Canada
>
> Code and aata below
> #================================================#
> library(reshape2)
> melt(mydata, id.vars = c("date"))
> #================================================#
>
> ######################DATA########################
> mydata <- structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(20L, 23L, 24L, 25L,
> 26L, 27L, 30L, 31L, 1L, 2L, 3L, 6L, 7L, 8L, 9L, 10L, 13L, 14L,
> 15L, 16L), mon = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), year = c(112L, 112L, 112L,
> 112L, 112L, 112L, 112L, 112L, 112L, 112L, 112L, 112L, 112L, 112L,
> 112L, 112L, 112L, 112L, 112L, 112L), wday = c(5L, 1L, 2L, 3L,
> 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L
> ), yday = c(19L, 22L, 23L, 24L, 25L, 26L, 29L, 30L, 31L, 32L,
> 33L, 36L, 37L, 38L, 39L, 40L, 43L, 44L, 45L, 46L), isdst = c(0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L)), .Names = c("sec", "min", "hour", "mday", "mon",
> "year", "wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt"
> )), open = c(36.445, 35.087, 34.859, 35.89, 36.544, 37.08, 36.336,
> 36.842, 36.961, 36.505, 36.911, 36.435, 37.139, 36.961, 36.921,
> 36.098, 36.326, 35.781, 35.404, 34.908), high = c(36.693, 35.206,
> 36.386, 36.197, 37.06, 37.427, 36.495, 37.635, 37.06, 36.604,
> 36.911, 37.09, 37.189, 37.189, 36.921, 36.197, 36.376, 35.9,
> 35.86, 35.503), low = c(35.305, 34.313, 34.531, 35.007, 35.702,
> 36.663, 35.702, 35.989, 36.306, 35.821, 36.257, 36.098, 36.118,
> 36.316, 36.207, 35.523, 35.483, 35.027, 34.839, 34.541), close = c(35.9,
> 35.146, 36.059, 36.108, 35.86, 36.792, 36.366, 36.475, 36.336,
> 36.237, 36.525, 37.09, 36.693, 36.713, 36.465, 35.761, 35.84,
> 35.404, 34.938, 35.345), x10d.sma = c(34.912, 34.958, 35.055,
> 35.063, 35.14, 35.375, 35.646, 35.936, 36.099, 36.128, 36.19,
> 36.385, 36.448, 36.509, 36.569, 36.466, 36.414, 36.306, 36.167,
> 36.077), x50d.sma = c(34.438, 34.373, 34.373, 34.357, 34.311,
> 34.284, 34.242, 34.2, 34.194, 34.207, 34.248, 34.323, 34.419,
> 34.525, 34.605, 34.655, 34.642, 34.628, 34.601, 34.574), x200d.sma = c(41.622,
> 41.559, 41.502, 41.445, 41.393, 41.354, 41.311, 41.266, 41.217,
> 41.166, 41.107, 41.048, 40.983, 40.917, 40.848, 40.778, 40.709,
> 40.638, 40.571, 40.512)), .Names = c("date", "open", "high",
> "low", "close", "x10d.sma", "x50d.sma", "x200d.sma"), row.names = c(NA,
> -20L), class = "data.frame")
> ###################################################
>
> sessionInfo()
> R version 2.15.2 (2012-10-26)
> Platform: i686-pc-linux-gnu (32-bit)
>
> locale:
> [1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C LC_TIME=en_CA.UTF-8
> [4] LC_COLLATE=en_CA.UTF-8 LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
> [7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C
> [10] LC_TELEPHONE=C LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] grid stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] lubridate_1.2.0 directlabels_2.9 RColorBrewer_1.0-5 gridExtra_0.9.1
> [5] stringr_0.6.2 scales_0.2.3 plyr_1.8 reshape2_1.2.2
> [9] ggplot2_0.9.3 quantmod_0.4-0 TTR_0.21-1 xts_0.9-3
> [13] zoo_1.7-9 Defaults_1.1-1
>
> loaded via a namespace (and not attached):
> [1] colorspace_1.2-1 dichromat_2.0-0 digest_0.6.2 gtable_0.1.2 labeling_0.1
> [6] lattice_0.20-13 MASS_7.3-23 munsell_0.4 proto_0.3-10 tools_2.15.2
>
> ____________________________________________________________
> TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if5
> Capture screenshots, upload images, edit and send them to your friends
> through IMs, post on Twitter®, Facebook®, MySpace™, LinkedIn® – FAST!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list