簡體   English   中英

使用recast()將復雜數據集從長到重整

[英]Reshaping a complex dataset from long to wide using recast()

我正在使用lme4附帶的數據集,並且我正在嘗試學習如何應用reshape2將其從long轉換為寬[在帖子末尾的完整代碼]。

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

數據集有9個變量; '憤怒','性別'和'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

關於重鑄如何定義id變量我可能不是100%清楚,但我很困惑為什么它沒有看到“憤怒”。 同樣的,

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.

我認為以下代碼可以滿足您的需求。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM