R重塑投射错误

时间:2014-11-25 11:24:24

标签: r reshape

我正在尝试按照以下示例进行重塑包,但收到错误

smithsm <- melt(smiths)

smithsm

 subject variable value
1 John Smith     time  1.00
2 Mary Smith     time  1.00
3 John Smith      age 33.00
4 Mary Smith      age    NA
5 John Smith   weight 90.00
6 Mary Smith   weight    NA
7 John Smith   height  1.87
8 Mary Smith   height  1.54

cast(smithsm, time + subject ~ variable)

这给出错误“错误:铸造公式包含熔融数据中未找到的变量:时间”。有谁知道导致此错误的原因是什么?以上是从一个例子

逐字逐句采取的

谢谢!

1 个答案:

答案 0 :(得分:1)

smithsm数据集没有time列。目前尚不清楚预期的wide形式是什么。也许,这有助于

 library(reshape2)
 dcast(smithsm, subject~variable, value.var='value')
 #    subject age height time weight
 #1 John Smith  33   1.87    1     90
 #2 Mary Smith  NA   1.54    1     NA

数据

 smithsm <- structure(list(subject = c("John Smith", "Mary Smith", "John Smith", 
 "Mary Smith", "John Smith", "Mary Smith", "John Smith", "Mary Smith"
 ), variable = c("time", "time", "age", "age", "weight", "weight", 
 "height", "height"), value = c(1, 1, 33, NA, 90, NA, 1.87, 1.54
 )), .Names = c("subject", "variable", "value"), class = "data.frame", 
 row.names = c("1", "2", "3", "4", "5", "6", "7", "8"))