需要帮助重塑 R 中的分组变量

Need Help reshaping grouped variables in R

对于在 R 中重塑数据的一些有用建议,我真的很感激。我一直在查看有关使用分组变量在宽格式和长格式之间重塑的相关讨论,但似乎无法让我的数据框在没有重塑的情况下重塑收到错误。 数据框看起来像这样...

mydata
                 Time  GRC1 Height1 GBL2 Height2 GPG3 Height3
1 1899-12-30 10:32:00 Vocal       h    M       m    M       m
2 1899-12-30 10:42:00 Vocal       m    M       m    R       m
3 1899-12-30 10:52:00     R       m    M       m  OOS      NA
4 1899-12-30 11:02:00     M       m    R       m    R       m

> dput(mydata)
structure(list(Time = structure(c(-2209123680, -2209123080, -2209122480, 
-2209121880), class = c("POSIXct", "POSIXt"), tzone = "GMT"), 
    GRC1 = structure(c(3L, 3L, 2L, 1L), .Label = c("M", "R", 
    "Vocal"), class = "factor"), Height1 = structure(c(1L, 2L, 
    2L, 2L), .Label = c("h", "m"), class = "factor"), GBL2 = structure(c(1L, 
    1L, 1L, 2L), .Label = c("M", "R"), class = "factor"), Height2 = structure(c(1L, 
    1L, 1L, 1L), .Label = "m", class = "factor"), GPG3 = structure(c(1L, 
    3L, 2L, 3L), .Label = c("M", "OOS", "R"), class = "factor"), 
    Height3 = structure(c(1L, 1L, 2L, 1L), .Label = c("m", "NA"
    ), class = "factor")), .Names = c("Time", "GRC1", "Height1", 
"GBL2", "Height2", "GPG3", "Height3"), row.names = c(NA, 4L), class = "data.frame")

我希望数据看起来像这样...

我目前可以管理的唯一方法是将 mydata 子集化为更小的数据框,融化数据,然后将所有内容重新绑定在一起。我觉得有一个更好的方法,我只是无法弄清楚。感谢您的任何建议。

假设你的初始数据集保存为dd,你可以这样做:

library(reshape2)

# reshape data
dd2 =
    reshape(dd, direction="long", idvar=c("Time"),
            varying = list(c("GRC1", "GBL2", "GPG3"), 
                           c("Height1", "Height2", "Height3")), 
            v.names = c("Behavior","Height"), times = c("GRC","GBL","GPG"))

# get rid of row names
row.names(dd2)=NULL

# rename column "time" to "Individual"
names(dd2)[which(names(dd2)=="time")] = "Individual"

# order by "Time"
dd2[order(dd2$Time),]

#                   Time Individual Behavior Height
# 1  1899-12-30 10:32:00        GRC    Vocal      h
# 5  1899-12-30 10:32:00        GBL        M      m
# 9  1899-12-30 10:32:00        GPG        M      m
# 2  1899-12-30 10:42:00        GRC    Vocal      m
# 6  1899-12-30 10:42:00        GBL        M      m
# 10 1899-12-30 10:42:00        GPG        R      m
# 3  1899-12-30 10:52:00        GRC        R      m
# 7  1899-12-30 10:52:00        GBL        M      m
# 11 1899-12-30 10:52:00        GPG      OOS     NA
# 4  1899-12-30 11:02:00        GRC        M      m
# 8  1899-12-30 11:02:00        GBL        R      m
# 12 1899-12-30 11:02:00        GPG        R      m