-1

I would really appreciate some helpful suggestions for reshaping data in R. I've been looking at related discussions on reshaping between wide and long format with grouped variables, but can't seem to get my data frame to reshape without receiving an error. The data frame looks like this...

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")

I would like the data to look like this...

enter image description here

The only way I can currently manage is to subset mydata into smaller data frames, melt the data, and then rbind everything back together. I feel like there is a better way that I just haven't been able to figure out. Thanks for any suggestions.

giderk
  • 61
  • 1
  • 7
  • 3
    Please provide a `dput` of your data.frame, not screenshots. –  Sep 18 '15 at 07:51
  • I'm not sure how to do that. I created a dput, but when I copy it from my r console to stackoverflow the code is not correctly formatted, and I'm not permitted to post. My intention was not to use pictures, I know it is not ideal. – giderk Sep 18 '15 at 08:24
  • If your data.frame is called `df`, run `dput(df)`, then copy the output of this command and paste it here. –  Sep 18 '15 at 08:27
  • Thank you for letting me know how. I just fixed it, and if you have any thoughts I'll be keen to hear them. – giderk Sep 18 '15 at 09:08

1 Answers1

3

Assuming that your initial dataset is saved as dd, you can do this:

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
AntoniosK
  • 15,991
  • 2
  • 19
  • 32