我想从一个没有丢失的数据点的数组中进行推断。解决方案是使所有值与不缺少的数据点相同。
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))))]}有人想到办法了吗?谢谢!
发布于 2015-03-17 09:45:24
试试这个:
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 0https://stackoverflow.com/questions/29095571
复制相似问题