我想用"行距离"到最近的非NA值填充缺失值。换句话说,如何将此示例数据框中的X列转换为y列?
# x y
#1 0 0
#2 NA 1
#3 0 0
#4 NA 1
#5 NA 2
#6 NA 1
#7 0 0
#8 NA 1
#9 NA 2
#10 NA 3
#11 NA 2
#12 NA 1
#13 0 0
我似乎找不到dplyr group_by和mutate row_number()语句的正确组合来解决这个问题。我研究的各种插补软件包都是为更复杂的场景而设计的,这些场景是使用统计和其他变量进行插补的。
d<-data.frame(x=c(0,NA,0,rep(NA,3),0,rep(NA,5),0),y=c(0,1,0,1,2,1,0,1,2,3,2,1,0))
我们可以使用
d$z = sapply(seq_along(d$x), function(z) min(abs(z - which(!is.na(d$x)))))
# x y z
# 1 0 0 0
# 2 NA 1 1
# 3 0 0 0
# 4 NA 1 1
# 5 NA 2 2
# 6 NA 1 1
# 7 0 0 0
# 8 NA 1 1
# 9 NA 2 2
# 10 NA 3 3
# 11 NA 2 2
# 12 NA 1 1
# 13 0 0 0
如果您想在dplyr中执行此操作,则只需将sapply
部分包装在mutate
中即可。
d %>%
mutate(z = sapply(seq_along(x), function(z) min(abs(z - which(!is.na(x))))))
或者,还使用library(purrr)
(感谢@ynyambu):
d %>% mutate(m=map_dbl(1:n(),~min(abs(.x-which(!is.na(x))))))
这是一种使用data.table
library(data.table)
setDT(d)
d[, out := pmin(cumsum(is.na(x)), rev(cumsum(is.na(x)))), by = rleid(is.na(x))]
d
# x y out
# 1: 0 0 0
# 2: NA 1 1
# 3: 0 0 0
# 4: NA 1 1
# 5: NA 2 2
# 6: NA 1 1
# 7: 0 0 0
# 8: NA 1 1
# 9: NA 2 2
#10: NA 3 3
#11: NA 2 2
#12: NA 1 1
#13: 0 0 0
对于每组NA
S,我们计算cumsum(is.na(x))
的平行最小值及其反向。之所以起作用,是因为所有非NA
s组中的值将为0
。如果要继续使用data.frame
,请致电setDF(d)
。
而不是两次计算cumsum(is.na(x))
,我们也可以做
d[, out := {
tmp <- cumsum(is.na(x))
pmin(tmp, rev(tmp))
}, by = rleid(is.na(x))]
这可能会带来性能,但我没有测试。
使用dplyr
语法这将读取
library(dplyr)
d %>%
group_by(grp = data.table::rleid(is.na(x))) %>%
mutate(out = pmin(cumsum(is.na(x)), rev(cumsum(is.na(x))))) %>%
ungroup()
# A tibble: 13 x 4
# x y grp out
# <dbl> <dbl> <int> <int>
# 1 0 0 1 0
# 2 NA 1 2 1
# 3 0 0 3 0
# 4 NA 1 4 1
# 5 NA 2 4 2
# 6 NA 1 4 1
# 7 0 0 5 0
# 8 NA 1 6 1
# 9 NA 2 6 2
#10 NA 3 6 3
#11 NA 2 6 2
#12 NA 1 6 1
#13 0 0 7 0
base R
rle_x <- rle(is.na(d$x))
grp <- rep(seq_along(rle_x$lengths), times = rle_x$lengths)
transform(d, out = ave(is.na(x), grp, FUN = function(i) pmin(cumsum(i), rev(cumsum(i)))))
在这里使用vapply
d$y <- 0
d$y[is.na(d$x)] <- vapply(which(diff(cumsum(is.na(d$x))) != 0),
function (k) min(abs(which(diff(cumsum(is.na(d$x))) == 0) - k)),
numeric(1))
d
x y
1 0 0
2 NA 1
3 0 0
4 NA 1
5 NA 2
6 NA 1
7 0 0
8 NA 1
9 NA 2
10 NA 3
11 NA 2
12 NA 1
13 0 0
d <- structure(list(x = c(0, NA, 0, NA, NA, NA, 0, NA, NA, NA, NA, NA, 0)),
class = "data.frame", row.names = c(NA, -13L))