我希望从只有一个非缺失数据点的数组中进行推断。解决方案是使所有值与非缺失数据点相同。
library(zoo)
datax <- data.frame(id = c(1:6),variable = c(NA,NA,0,NA,NA,NA))
#Both na.fill, and na.approx require at least two data points in the vector,
#in the case of interpolation naturally for a good reason.
datax$variable <- na.fill(na.approx(datax$variable, na.rm = FALSE), "extend")
我可以写下面的黑客,但我想知道是否有更好更通用的功能。
if(length(which(!is.na(as.numeric(unlist(datax$variable))))) == 1) +
{datax$variable <- datax[which(!is.na(as.numeric(unlist(datax$variable))))]}
有人有个主意吗?谢谢!
答案 0 :(得分:2)
试试这个:
library(zoo)
library(dplyr)
# From zoo::na.locf() description:
# Generic function for replacing each NA with the most recent non-NA prior to it.
datax$new_variable <-
na.locf(na.locf(datax$variable, na.rm = FALSE), fromLast = TRUE)
datax %>%
mutate(new_variable = na.locf(na.locf(variable, na.rm = FALSE), fromLast = TRUE))
id variable new_variable
1 1 NA 0
2 2 NA 0
3 3 0 0
4 4 NA 0
5 5 NA 0
6 6 NA 0