How to create missing value for repeated measurement data?

前端 未结 2 1457
鱼传尺愫
鱼传尺愫 2021-01-06 03:40

I have a data set that not every subject’s observations were observed at the exact same time points, but I want to turn it in to a data set that every one’s observations wer

2条回答
  •  爱一瞬间的悲伤
    2021-01-06 04:04

    Using tidyr, this is a one liner. You use the complete function, which creates rows with each combination of the columns passed to it, filling the rest of the rows with NA:

    library(tidyr)
    complete(m, id, age)
    
    Source: local data frame [18 x 3]
    
          id   age    IQ
       (dbl) (dbl) (dbl)
    1      1     2     3
    2      1     3     4
    3      1     4     5
    4      1     5     4
    5      1     6    NA
    6      1     8    NA
    7      2     2    NA
    8      2     3     6
    9      2     4    NA
    10     2     5    NA
    11     2     6     5
    12     2     8    NA
    13     3     2     3
    14     3     3    NA
    15     3     4    NA
    16     3     5     8
    17     3     6    NA
    18     3     8    10
    

提交回复
热议问题