select last observation from longitudinal data

前端 未结 6 1217
小蘑菇
小蘑菇 2020-12-28 09:19

I have a data set with several time assessments for each participant. I want to select the last assessment for each participant. My dataset looks like this:

         


        
6条回答
  •  心在旅途
    2020-12-28 10:15

    If you're just looking for the last observation per person ID, then a simple two line code should do it. I am up always for simple base solution when possible while it is always great to have more than one ways to solve a problem.

    dat[order(dat$ID,dat$Week),]  # Sort by ID and week
    dat[!duplicated(dat$ID, fromLast=T),] # Keep last observation per ID
    
       ID Week Outcome
    3   1    6      42
    8   4   12      85
    13  9   12      84
    

提交回复
热议问题