更改最大差值以结转值的次数 [英] Change maxgap for number of times a value is carried forward

查看:100
本文介绍了更改最大差值以结转值的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下内容的数据框:

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)) )

在此数据帧中,我有一个变量,该变量指示我可以进行最后一次观察的时间(locf_N).这不是所有观察值的固定数字.我试图为此目的在na.locf函数中使用maxgap参数,但这实际上并不是我想要的.

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.

所需的输出:

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)) )

推荐答案

cumsum(!is.na(value))是一个分组向量,用于将每个非NA与以下NA进行分组.然后,对于每个这样的组,将第一个值重复所需的次数,并将其余值保留为NA.

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]

给予:

    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

这篇关于更改最大差值以结转值的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆