Я пытаюсь использовать функцию 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
Код, который я использовал, представлен ниже.
#read the excel file and the sheet of interest
test= structure(list(`1243_4sel` = c("2.3525000000000001E-2", "5.6603000000000001E-2", "9.1589000000000004E-2", "8.6460999999999996E-2",
"1.7899999999999999E-3","1.8776000000000001E-2", "NA"), `1245_4sel` = c("2.6909999999999998E-3",
"1.7314E-2", "-4.8430000000000001E-3", "4.0668999999999997E-2", "-1.0984000000000001E-2",
"-4.2880000000000001E-3", "NA"), `1255_4sel` = c("-2.6103000000000001E-2","5.2512999999999997E-2",
"-2.1322000000000001E-2", "-3.5166999999999997E-2", "4.4469000000000002E-2", "4.6221999999999999E-2", "NA"),
`1265_4sel` = c("NA", "-2.8042999999999998E-2", "NA", "NA", "-5.4059000000000003E-2", "-6.4116000000000006E-2", "NA"),
`1266_4sel` = c("NA", "3.6759E-2", "NA", "NA", "-7.7029999999999998E-3", "1.9910000000000001E-3", "NA"),
`1268_4sel` = c("-8.8400000000000002E-4", "6.5909999999999996E-2", "1.9495999999999999E-2", "6.9381999999999999E-2", "-2.764E-3",
"-3.6695999999999999E-2", "NA"), `1269_4sel` = c("-2.4181000000000001E-2", "2.1572000000000001E-2",
"-1.6182999999999999E-2", "2.5044E-2", "-3.3234E-2", "-1.6448999999999998E-2", "NA"),
`1274_4sel` = c("-2.0017E-2", "3.5234000000000001E-2", "-3.8670999999999997E-2", "5.6230000000000004E-3",
"-1.7319000000000001E-2", "1.4264000000000001E-2", "NA"), `1276_4sel` = c("-4.1009999999999996E-3", "6.1688E-2", "1.3915E-2", "1.0439E-2",
"5.1450000000000003E-3","3.5539999999999999E-3", "NA"), `1277_4sel` = c("2.8437E-2", "2.0038E-2", "5.1650000000000001E-2", "9.8672999999999997E-2",
"-3.5707999999999997E-2", "1.7539999999999999E-3", "NA"), `1278_4sel` = c("3.4150000000000001E-3", "4.5303999999999997E-2", "2.818E-2",
"6.0167999999999999E-2","-2.5447999999999998E-2", "-4.061E-2", "NA"), `1387_4sel` = c("NA", "-8.8667999999999997E-2", "-3.0911999999999999E-2",
"NA", "-4.4260000000000001E-2", "-2.3598999999999998E-2", "NA")), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"))
#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())
na.spline(df)
Я не знаю, нужно ли мне читать файл excel как зоопарк объект или нет для вызова функции na.spline, но даже если я это сделаю, я получаю другую ошибку
Error in read.zoo(df) : index has bad entries at data rows: 7 8 21
Я обнаружил в других сообщениях, что проблема вызвана, потому что некоторые rows
начинаются с NA
's. Есть мысли?