-1

I have a longitudinal data which included 1000 patients and four variables. These variables are repeatedly measured over time. The time differs across patients. My question is how to line up all patients by their final time in R?
Here an example of the data :

    dput(head(mydata))
structure(list(Enum = c(15306L, 15306L, 15306L, 15306L, 11056L, 
    11056L), Sex = c(0L, 0L, 0L, 0L, 1L, 1L), Left_R = c(1L, 1L, 
    1L, 1L, 0L, 0L), Right_R = c(1L, 1L, 1L, 1L, 0L, 0L), risk = c(2, 
    1.5, 1.5, 1.5, 0, 0), Chol_val = c(4.1, 3.7, 3.9, 3.7, 5.2, 3.6
    ), HbA1c_val = c(39L, 41L, 43L, 39L, 39L, 45L), eGFR_val = c(90, 
    NA, 90, 85, 82, 85), Duration = c(3682L, 3682L, 3682L, 3682L, 
    36L, 36L), Age = c(65L, 65L, 65L, 65L, 38L, 38L), time = c(0L, 
    392L, 756L, 1125L, 0L, 351L), BMI = c(23.301094832264, 22.4392735165502, 
    21.9604838967091, 22.6627066115703, 39.3535698314, 39.7355371900827
    ), Status = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Enum", "Sex", 
    "Left_R", "Right_R", "risk", "Chol_val", "HbA1c_val", "eGFR_val", 
    "Duration", "Age", "time", "BMI", "Status"), row.names = c(NA, 
    6L), class = "data.frame")
Dave2e
  • 22,192
  • 18
  • 42
  • 50
R. Saeiti
  • 89
  • 3
  • 11

1 Answers1

0

If I understand the question correctly you wish to order the patiants by the maximum time they gave a measurement

library(dplyr)
test <- df %>% group_by(Enum) %>% mutate(maxtime = max(time)) %>% 
ungroup %>% arrange(maxtime, Enum) %>% select(-maxtime)

if you wish to order them from largest time to smallest time use arrange(desc(maxtime), Enum).

Jonno Bourne
  • 1,931
  • 1
  • 22
  • 45