【问题标题】:R reshape cast errorR重塑铸造错误
【发布时间】:2014-11-25 11:24:24
【问题描述】:

我正在尝试按照以下示例进行 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)

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

谢谢!

【问题讨论】:

  • 这确实是一个很好的错误消息,您的 data.frame 中没有名为 time 的列。

标签: r reshape


【解决方案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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2016-12-18
    • 1970-01-01
    • 2011-02-07
    • 2012-12-20
    • 1970-01-01
    • 2013-05-09
    相关资源
    最近更新 更多