R,从一个数据点推断

R, extrapolate from one data point

我希望从只有一个非缺失数据点的数组中进行推断。解决方案是使所有值与非缺失数据点相同。

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

我可以编写以下 hack,但我想知道是否有更好更通用的函数。

if(length(which(!is.na(as.numeric(unlist(datax$variable))))) == 1) +
    {datax$variable <- datax[which(!is.na(as.numeric(unlist(datax$variable))))]}

有人知道吗?谢谢!

试试这个:

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)

动物园和 dplyr

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