【问题标题】:R error in melt (reshape) - number of rows熔体中的 R 错误(重塑) - 行数
【发布时间】:2012-07-10 11:02:37
【问题描述】:

看看这个包含五个观察值和两个因子变量的测试数据集。

test

  respid                  s2q1                      s2q2
1     32                     9          5 - V. satisfied
2     35       10 - Definitely          5 - V. satisfied
3    148       10 - Definitely          5 - V. satisfied
4    371                     3                         2
5    416       10 - Definitely          5 - V. satisfied

当我运行以下melt 命令时,我收到一个对我来说没有意义的错误。特别是因为我之前成功使用过相同的命令。

require(reshape2)
qhist <- melt(test, id="respid")

Error in data.frame(ids, variable, value, stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 5, 10

数据是用

导入的
spss.get("data.sav", lowernames=T, use.value.labels=T,max.value.labels=13)

dput(test)
structure(list(respid = structure(c(32L, 35L, 148L, 371L, 416L), label = structure("Serial Number", .Names = "respid"), class = "labelled"), 
    s2q1 = structure(c(10L, 11L, 11L, 4L, 11L), .Label = c("0 - Definitely not", 
    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10 - Definitely", 
    "Don't know"), class = c("labelled", "factor"), label = structure("S2Q1 xxx?", .Names = "s2q1")), 
    s2q2 = structure(c(5L, 5L, 5L, 2L, 5L), .Label = c("1 - Not at all satisfied", 
    "2", "3", "4", "5 - V. satisfied", "Don't know"), class = c("labelled", 
    "factor"), label = structure("S2Q2 xxx?", .Names = "s2q2"))), .Names = c("respid", 
"s2q1", "s2q2"), row.names = c(NA, 5L), class = "data.frame")

【问题讨论】:

  • 请将dput(test) 的结果发布到您的问题中,以便我们可以准确地查看您的数据是什么样的。
  • 我不得不审查 xxx 的实际问题。
  • 我无法重现错误。
  • hm.. 我正在使用带有 R 2.15.0 的 RStudio?
  • 我之前加载了reshape,但没有意识到reshape2没有重新定义melt。

标签: r reshape


【解决方案1】:

这是由respid 的结构引起的,即因为它的类被“标记”了,但这是有效的:

library(reshape)
reshape::melt(test, id="respid")
   respid variable            value
1      32     s2q1                9
2      35     s2q1  10 - Definitely
3     148     s2q1  10 - Definitely
4     371     s2q1                3
5     416     s2q1  10 - Definitely
6      32     s2q2 5 - V. satisfied
7      35     s2q2 5 - V. satisfied
8     148     s2q2 5 - V. satisfied
9     371     s2q2                2
10    416     s2q2 5 - V. satisfied

或者如果不使用reshape 那么

class(test$respid) <- "factor"
reshape2::melt(test, id="respid")

也可以。

【讨论】:

  • 谢谢,你能快速解释一下::吗?
  • 在这种情况下,我们在两个包中都有函数melt,所以使用::,我们指定要使用哪个包。
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2017-06-24
相关资源
最近更新 更多