0

I want to perform several repeated measures on my data. I first need to reshape the dataframe from a wide to a long format to do that.

This is my dataframe:

    ID Group x1  x2  x3  y1  y2  y3  z1  z2
    144 1   566 613 597 563 549 562 599  469
    167 2   697 638 756 682 695 693 718  439.5
    247 4   643 698 730 669 656 669 698  514.5
    317 4   633 646 641 520 543 586 559  405.5
    344 3   651 678 708 589 608 615 667  514
    352 2   578 702 671 536 594 579 591  467.5
    382 1   678 690 693 555 565 534 521  457.5
    447 3   668 672 718 663 689 751 784  506.5
    464 2   760 704 763 514 554 520 564  486
    628 1   762 789 783 618 610 645 625  536

As you might notice, I have measured variable x and y on three time points and variable z at two points. I was wondering if it makes sense at all to try and reshape the data into long format, given the fact that I have separate time lengths.

I have not been able to do so. So first of all, does it even make sense to do it this way? Or should I make two dataframes? Second, if it does make sense, how?

EDIT: I would expect something like:

ID Group Timex  Timey Timez  x    y    z
144 1     1       1     1   566  563  599
144 1     2       2     2   613  549  469
144 1     3       3         597  562
167 2     1       1     1   697  682  718
167 2     2       2     2   638  695  439.5 
167 2     3       3         756  693
....

But I'm not even sure if that makes sense at all, to have these empty cells?

Inkling
  • 469
  • 1
  • 4
  • 19
  • Can u show the expected output? – akrun Jun 14 '17 at 10:31
  • i'm with akrun on this one. what do you want to achieve? – tagoma Jun 14 '17 at 11:02
  • I added my expectation. I did not include that first, because I am not even sure if this is possible or if it even makes sense! – Inkling Jun 14 '17 at 11:33
  • @Inkling After I posted my answer, I just noticed that you asked a similar question before (https://stackoverflow.com/questions/43850034/how-do-i-use-the-reshape-function-more-than-once-succesfully-in-r) and this questions has been marked as a duplicate. Is there any reason why you posted a similar question again, especially it is a duplicate? – www Jun 14 '17 at 16:12
  • Thanks for your answer! To me, these we two separate questions, my other question was on how to use the reshape function for multiple variables at once (which I did not notice before it was a duplicate, I could remove my question if that's common practice? ). My question here was specifically on the fact if the variables have different lengths. (I used a similar dataset as illustration, but notice I left out z3 in this example). After my first question got answered, I got stuck with this problem, so to me, this was a follow up. – Inkling Jun 14 '17 at 19:34
  • Thanks for your explanation. Unless there are other concerns, I feel like you don't have to remove your last question. There were others giving nice answers to your question. If you remove that, all the associated answers will be gone. – www Jun 14 '17 at 19:56

1 Answers1

0

Here is one idea. dt_all is the final output. Notice that this example does not create Timex, Timey, and Timez, but I would argue that one column called Time is sufficient and individual Timex, Timey, and Timez are redundant.

# Load packages
library(dplyr)
library(tidyr)

# Process the data
dt_all <- dt %>%
  gather(Var, Value, -ID, -Group) %>%
  mutate(Time = sub("[a-z]", "", Var), Type = sub("[0-9]", "", Var)) %>%
  select(-Var) %>%
  spread(Type, Value)

Data Preparation

# Create example data frames
dt <- read.table(text = "ID Group x1  x2  x3  y1  y2  y3  z1  z2
    144 1   566 613 597 563 549 562 599  469
                 167 2   697 638 756 682 695 693 718  439.5
                 247 4   643 698 730 669 656 669 698  514.5
                 317 4   633 646 641 520 543 586 559  405.5
                 344 3   651 678 708 589 608 615 667  514
                 352 2   578 702 671 536 594 579 591  467.5
                 382 1   678 690 693 555 565 534 521  457.5
                 447 3   668 672 718 663 689 751 784  506.5
                 464 2   760 704 763 514 554 520 564  486
                 628 1   762 789 783 618 610 645 625  536",
                 header = TRUE)
www
  • 38,575
  • 12
  • 48
  • 84