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

时间:2015-09-18 07:40:35

标签: r reshape reshape2

我真的很感激在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")

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

enter image description here

我目前管理的唯一方法是将mydata子集化为更小的数据帧,融化数据,然后将所有内容重新组合在一起。我觉得有一种更好的方式让我无法理解。感谢您的任何建议。

1 个答案:

答案 0 :(得分:2)

假设您的初始数据集保存为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