2015-12-15 54 views
2

我有一個數據幀,其中包含一段時間內對相同主題的重複測量。我想找到的是將這些數據從非單調缺失轉換爲單調缺失的簡單方法。將非單音數據轉換爲單調數據R

例如id 1是單調的:1,NA,NA,NA id 3是非單調的:0,NA,0,0 id 4是非單調的:1,1,NA,1

我要ID = 3爲Y = c(0,NA,NA,NA),id = 4爲Y = c(1,1,NA,NA)。

對此提出建議?

玩具數據集包括:

df=structure(list(id = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L), Y = c(1L, 1L, 0L, 1L, NA, 1L, NA, 1L, 
NA, 1L, 0L, NA, NA, 0L, 0L, 1L), X = c(5L, 6L, 7L, 8L, 3L, 5L, 
6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L), t = c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L)), .Names = c("id", 
"Y", "X", "t"), class = "data.frame", row.names = c(NA, -16L)) 

回答

3

你可以試試:

df$newcol<-ave(df$Y,df$id,FUN=function(x) NA^is.na(cumsum(x))*x) 
# id Y X t newcol 
#1 1 1 5 1  1 
#2 2 1 6 1  1 
#3 3 0 7 1  0 
#4 4 1 8 1  1 
#5 1 NA 3 2  NA 
#6 2 1 5 2  1 
#7 3 NA 6 2  NA 
#8 4 1 7 2  1 
#9 1 NA 8 3  NA 
#10 2 1 9 3  1 
#11 3 0 1 3  NA 
#12 4 NA 2 3  NA 
#13 1 NA 3 4  NA 
#14 2 0 4 4  0 
#15 3 0 5 4  NA 
#16 4 1 6 4  NA 
+1

你id和時間的數據框,然後這兩個工作。 +1 –

5

您可以使用is.na <-cumsum傳播NA值:排序只要

df <- within(df, Y <- ave(Y, id, FUN = function(x) { 
    is.na(x) <- is.na(cumsum(x)) 
    x 
})) 

df[order(df$id),] 
# id Y X t 
# 1 1 1 5 1 
# 5 1 NA 3 2 
# 9 1 NA 8 3 
# 13 1 NA 3 4 
# 2 2 1 6 1 
# 6 2 1 5 2 
# 10 2 1 9 3 
# 14 2 0 4 4 
# 3 3 0 7 1 
# 7 3 NA 6 2 
# 11 3 NA 1 3 
# 15 3 NA 5 4 
# 4 4 1 8 1 
# 8 4 1 7 2 
# 12 4 NA 2 3 
# 16 4 NA 6 4 
+0

加1。你也可以使用'replace'來使它成爲一行:'replace(x,is.na(cumsum(x)),NA)' –