繁体   English   中英

在reshape2中使用melt()的尺寸不匹配

[英]Dimension Mismatch using melt() in reshape2

我正在尝试使用包'reshape2'中的函数'melt'将R中的'宽'格式的数据帧融为'长'格式。 但是,尝试查看输出数据帧时遇到了尺寸方面的问题,我无法对其进行解密。 这是一个例子:

# 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)

但是,当我尝试查看df.melt.id数据帧时,出现以下错误:

Error in FUN(X[[i]], ...) : 
  dims [product 4] do not match the length of object [12]

4对应于数据帧的原始长度,而12则应为多长时间。 如果我使用dim(df.melt.id)检查尺寸,它将返回适当的尺寸: [1] 12 3

对这里发生的事情有什么想法吗? 我尝试重新安装reshape2,但没有帮助...

当您执行此reshape2时,它可与reshape2一起使用:

df.melt.id <- as.data.frame.array(melt(df, id="year"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM