简体   繁体   中英

Reshaping data of different time lengths in R

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM