4

5つの観測値と2つの因子変数を含むこのテストデータセットを見てください。

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")
4

1 に答える 1

5

これは の構造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")

あまりにも動作します。

于 2012-07-10T11:53:25.637 に答える