我正在尝试使用na.spline
程序包中的zoo
函数,以便在NA
中内插一些data frame
值。我收到以下错误:
Error in splinefun(x[!na], y[!na], ...) : zero non-NA points
In addition: Warning message:
In xy.coords(x, y, setLab = FALSE) : NAs introduced by coercion
下面显示了我所使用的代码,并且可以找到数据样本here。
#read the excel file and the sheet of interest
df = read_excel("2017_NDVI_Anomaly_Zonal_Stats.xlsx", sheet = "Sheet4")
#convert the column Date from character to date format
df$DATE <- as.Date(df$DATE, format = "%y-%m-%d")
str(df$DATE)
#remove the 1st column of the excel
df <- subset(df, select = -c(RowID))
#convert the character columns to numeric
df %<>% mutate_if(is.character,as.numeric)
class(df$DATE)
#check the format of the columns
df[] <- lapply(df, function(x) {
if(is.factor(x)) as.numeric(as.character(x)) else x
})
sapply(df, class)
#convert all character cells to null
df[is.character(df)]= NULL
View(df)
df[which(is.character(df))] <- NULL
sum(is.na(df))
#move the last column to the 1st position
df %>% select(0:0, length(df), everything())
dput(df[1:7,30:41])
na.spline(df)
我不知道是否必须将Excel文件作为Zoo对象读取才能调用na.spline函数,但是即使这样做,我也会遇到另一个错误
Error in read.zoo(df) : index has bad entries at data rows: 7 8 21
我在其他帖子上发现该问题是由于某些rows
以NA
开头而引起的。有什么想法吗?
您可能需要使用lapply
逐列应用na.spline
。
dat[-1] <- lapply(dat[-1], zoo::na.spline)
dat
# DATE X155_4sel X964_4sel X970_4sel
# 1 2016-12-02 -0.0162270 -0.022269 0.095243
# 2 2016-12-10 0.1005000 0.041044 -0.021599
# 3 2016-12-18 -0.0064720 0.039549 0.161545
# 4 2016-12-26 -0.0386020 0.059017 0.159219
# 5 2017-01-01 0.1657300 -0.046500 -0.054670
# 6 2017-01-09 0.1582580 -0.017922 -0.079368
# 7 2017-01-17 -0.5417341 -0.110800 -0.107872 ## dat[7, 2] is extrapolated
数据
dat <- structure(list(`155_4sel` = c("-0.016226999999999998", "0.10050000000000001",
"-0.0064720000000000003", "-0.038601999999999997", "0.16572999999999999",
"0.15825800000000001", "NA"), `964_4sel` = c(-0.022269, 0.041044,
0.039549, 0.059017, -0.0465, -0.017922, -0.1108), `970_4sel` = c(0.095243,
-0.021599, 0.161545, 0.159219, -0.05467, -0.079368, -0.107872
), DATE = structure(c(1480636800, 1481328000, 1482019200, 1482710400,
1483228800, 1483920000, 1484611200), class = c("POSIXct", "POSIXt"
), tzone = "UTC")), row.names = c(NA, -7L), class = "data.frame")
# dat <- as.data.frame(read_excel("2017_NDVI_Anomaly_Zonal_Stats.xlsx", sheet = "Sheet4"))[-1]
dat$DATE <- as.Date(dat$DATE, format="%y-%m-%d")
dat$`155_4sel` <- as.numeric(dat$`155_4sel`)
dat <- dat[c(4, 1:3)]
names(dat) <- make.names(names(dat)) ## You may want to use proper names (not beginning with number)