Alternatives to stats::reshape

后端 未结 2 1312
傲寒
傲寒 2021-02-10 08:14

The melt/cast functions in the reshape package are great, but I\'m not sure if there is a simple way to apply them when measured variables are of different types. For example,

2条回答
  •  臣服心动
    2021-02-10 08:29

    I think the reshape function in the stats package is simplest. Here is a simple example, does this do what you want?

    > tmp
      id val val2 cat
    1  1   1   14   a
    2  1   2   13   b
    3  2   3   12   b
    4  2   4   11   a
    > tmp2 <- tmp
    > tmp2$t <- ave(tmp2$val, tmp2$id, FUN=seq_along)
    > tmp2
      id val val2 cat t
    1  1   1   14   a 1
    2  1   2   13   b 2
    3  2   3   12   b 1
    4  2   4   11   a 2
    > reshape(tmp2, idvar='id', timevar='t', direction='wide')
      id val.1 val2.1 cat.1 val.2 val2.2 cat.2
    1  1     1     14     a     2     13     b
    3  2     3     12     b     4     11     a
    

    Hopefully your patients sex is not changing each appointment, but there could be other categorical variables that change between visits

提交回复
热议问题