【发布时间】:2014-06-04 17:02:20
【问题描述】:
当使用stats:::reshape() from base 将数据从长格式转换为宽格式时,对于任何指定为时间不变的变量,reshape 只取第一个观察值,如果变量实际上以某种方式变化,则输出警告。在我的情况下,我缺少关于我想指定为时间不变的变量的数据,但由于我在其他时间点有这些数据,我希望使用这些时间点的值而不是 NA 这是第一次观察到。
testdata <- data.frame(matrix(c(1,1,2,3,4,3.5,NA,6,4,1,2,1), nrow = 3))
colnames(testdata) <- c("id", "process1", "timeinvariant", "time")
# > testdata
# id process1 timeinvariant time
# 1 1 3.0 NA 1
# 2 1 4.0 6 2
# 3 2 3.5 4 1
# Note here the missing data on the invariant process at time 1
reshaped <- reshape(testdata, v.names="process1", direction = "wide")
# > reshaped
# id timeinvariant process1.1 process1.2
# 1 1 NA 3.0 4
# 3 2 4 3.5 NA
NA 传播到宽格式,而我宁愿采用在时间 2(或任何时候)观察到的值。
【问题讨论】: