Change maxgap for number of times a value is carried forward

笑着哭i 提交于 2020-01-06 02:38:36

问题


I have a data frame similar to the following:

library(data.table)
test <- data.table(data.frame("value" = c(5,NA,8,NA,NA,8,6,NA,NA,10),
                              "locf_N" = c(1,NA,1,NA,NA,1,2,NA,NA,2)) )

In this data frame I have a variable that indicates the times I could carry forward the last observation (locf_N). This is not a fixed number for all observations. I have tried to use the maxgap parameter in the na.locf function for this purpose but it is not actually what I am looking for.

require(zoo)
test[,value := na.locf(value, na.rm = FALSE, maxgap = 1)]
test[,value := na.locf(value, na.rm = FALSE, maxgap = locf_N)]

Is there any parameter to set the number of times the last observation can be carried forward? Any ideas welcome.

Desired output:

output <- data.table(data.frame("value" = c(5,5,8,8,NA,8,6,6,6,10),
                                "locf_N" = c(1,NA,1,NA,NA,1,2,NA,NA,2)) )

回答1:


cumsum(!is.na(value)) is a grouping vector that groups each non-NA with the following NAs. Then for each such group repeat the first value the required number of times and leave the remaining values as NA.

test[, list(value = replace(value, 1:min(.N, locf_N[1] + 1), value[1]), locf_N), 
        by = cumsum(!is.na(value))][, -1]

giving:

    value locf_N
 1:     5      1
 2:     5     NA
 3:     8      1
 4:     8     NA
 5:    NA     NA
 6:     8      1
 7:     6      2
 8:     6     NA
 9:     6     NA
10:    10      2


来源:https://stackoverflow.com/questions/50486104/change-maxgap-for-number-of-times-a-value-is-carried-forward

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