【问题标题】:Reshaping a complex dataset from long to wide using recast()使用 recast() 从长到宽重塑复杂数据集
【发布时间】:2012-11-27 18:49:06
【问题描述】:

我正在使用 lme4 附带的数据集,并且正在尝试学习如何应用 reshape2 将其从长转换为宽 [帖子末尾的完整代码]。

library(lme4)
data("VerbAgg")  # load the dataset

数据集有9个变量; 'Anger'、'Gender' 和 'id' 不随 'item' 而变化,而 'resp', 'btype'、'situ'、'mode' 和 'r2' 都可以。

我已经成功地使用 reshape() 将数据集从长格式转换为宽格式:

wide <- reshape(VerbAgg, timevar=c("item"), 
             idvar=c("id", 'Gender', 'Anger'), dir="wide")

对 123 个变量产生 316 个观测值,并且似乎已正确转换。但是,我没有成功使用 reshape/reshape2 来重现宽数据框。

wide2 <- recast(VerbAgg, id + Gender + Anger ~ item + variable)
Using Gender, item, resp, id, btype, situ, mode, r2 as id variables
Error: Casting formula contains variables not found in molten data: Anger

我可能不是 100% 清楚 recast 如何定义 id 变量,但我很困惑为什么它没有看到“愤怒”。同样,

wide3 <- recast(VerbAgg, id + Gender + Anger ~ item + variable, 
               id.var = c("id", "Gender", "Anger"))
Error: Casting formula contains variables not found in molten data: item

谁能看到我做错了什么?我很想更好地了解熔化/铸造!

完整代码:

## load the lme4 package 
library(lme4) 
data("VerbAgg")
head(VerbAgg)
names(VerbAgg) 

# Using base reshape()
wide <- reshape(VerbAgg, timevar=c("item"), 
                 idvar=c("id", 'Gender', 'Anger'), dir="wide")

# Using recast
library(reshape2)
wide2 <- recast(VerbAgg, id + Gender + Anger ~ item + variable)
wide3 <- recast(VerbAgg, id + Gender + Anger ~ item + variable, 
                id.var = c("id", "Gender", "Anger"))

# Using melt/cast
m <- melt(VerbAgg, id=c("id", "Gender", "Anger"))
wide <- o cast(m,id+Gender+Anger~...)
Aggregation requires fun.aggregate: length used as default
# Yields a list object with a length of 8? 

m <- melt(VerbAgg, id=c("id", "Gender", "Anger"), measure.vars = c(4,6,7,8,9))
wide <- dcast(m, id ~ variable)
# Yields a data frame object with 6 variables.

【问题讨论】:

  • +1 因为你了解融化/铸造
  • wide 对 26 个变量产生 316 个观察值??当我检查暗淡(宽)时,我有 316 行和 123 列。
  • 您最好先使用melting,然后再使用casting - 这样您就可以更轻松地发现问题所在。
  • agstudy,你是对的。正确的尺寸是 316 行和 123 列。抱歉打错了。
  • 哈德利,感谢您的回复!我们尝试了先融化然后再施放,但又出现了一些其他奇怪的行为。我更新了问题以反映我们的尝试。显然,我在定义熔化/铸造公式时遗漏了一些东西......有什么想法吗?

标签: r dataset reshape2


【解决方案1】:

我认为以下代码可以满足您的要求。

library(lme4) 
data("VerbAgg")

# Using base reshape()
wide <- reshape(VerbAgg, timevar=c("item"), 
                 idvar=c("id", 'Gender', 'Anger'), dir="wide")
dim(wide) # 316 123

# Using melt/cast
require(reshape2)
m1 <- melt(VerbAgg, id=c("id", "Gender", "Anger","item"), measure=c('resp','btype','situ','mode','r2'))
wide4 <- dcast(m1,id+Gender+Anger~item+variable)
dim(wide4) # 316 123

R> wide[1:5,1:6]
  Anger Gender id resp.S1WantCurse btype.S1WantCurse situ.S1WantCurse
1    20      M  1               no             curse            other
2    11      M  2               no             curse            other
3    17      F  3          perhaps             curse            other
4    21      F  4          perhaps             curse            other
5    17      F  5          perhaps             curse            other

R> wide4[1:5,1:6]
  id Gender Anger S1WantCurse_resp S1WantCurse_btype S1WantCurse_situ
1  1      M    20               no             curse            other
2  2      M    11               no             curse            other
3  3      F    17          perhaps             curse            other
4  4      F    21          perhaps             curse            other
5  5      F    17          perhaps             curse            other

【讨论】:

  • 谢谢!我没有意识到 item 应该作为 id 包含在内。看起来不错,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2018-05-14
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多