reshape from base vs dcast from reshape2 with missing values

橙三吉。 提交于 2019-12-01 20:29:15

The relevant part of the reshape code would be the line:

data[, tempidname] <- interaction(data[, idvar], drop = TRUE)

Look at how interaction works:

> interaction("A", "B")
[1] A.B
Levels: A.B
> interaction("A", "B", NA)
[1] <NA>
Levels: 

But, compare what would happen if NA were retained as a level:

> interaction("A", "B", addNA(NA))
[1] A.B.NA
Levels: A.B.NA

Thus, if you want to have the same result with base R's reshape, you need to make sure that any "idvar" columns have NA retained as a level.

Example:

df$sex <- addNA(df$sex)
reshape(df,
        timevar="visit",
        idvar=c("id", "parameter", "sex"),
        direction="wide")
#   id parameter  sex value.V1 value.V2 value.V3
# 1 01     blood <NA>        1       NA       NA
# 2 01    saliva <NA>        2       NA       NA

Of course, the other question is how NA can be treated as an identifying variable :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!