I am trying to melt a data frame from 'wide' format to 'long' format in R, using the function 'melt' in the package 'reshape2'. However, I am encountering an issue with dimensions when trying to view the output data frame which I am having trouble deciphering. Here is an example:
# load reshape2 package
require(reshape2)
# sample data frame generated using dput
df <- structure(list(year = c(2001, 2002, 2003, 2004),
aet = structure(c(493.1, 407.1, 476.7, 501.6), .Dim = 4L),
drainage = structure(c(5.4, 5.4, 5.4, 5.4), .Dim = 4L),
srunoff = structure(c(25.6, 24.3, 56.0, 50.3), .Dim = 4L)),
.Names = c("year", "aet", "drainage", "srunoff"), row.names = c(NA, 4L), class = "data.frame")
# if i melt without specifying id.vars, it provides a warning but works works fine
df.melt <- melt(df)
# check output
head(df.melt)
# view output
View(df.melt)
# this works fine, and the data frame is visible in RStudio
# now, melt while supplying year as an id variable
df.melt.id <- melt(df, id.vars="year")
# check output
head(df.melt.id)
# the first 6 lines of output print to the console menu, as normal
# view output
View(df.melt.id)
However, when I try to view the df.melt.id data frame, I get the following error:
Error in FUN(X[[i]], ...) :
dims [product 4] do not match the length of object [12]
4 corresponds to the original length of the data frame, and 12 is how long it should be. If I check the dimensions using dim(df.melt.id)
, it returns the appropriate size: [1] 12 3
Any ideas on what's going on here? I have tried re-installing reshape2 and that did not help...