library(seastests)
paste_noNA <- function(x) {
ts(x[!is.na(x)],frequency=12)
}
a <- data.frame(a=c(1,2),b=c(2,5),c=c(10,2),
d=c(9,22),e=c(6,3),f=c(5,7),
g=c(2,12),h=c(9,7),i=c(8,8),
j=c(4,21),k=c(NA,7),l=c(4,2),
m=c(7,3),n=c(11,8),o=c(7,8),
p=c(9,6),q=c(10,9),r=c(8,9),s=c("f","h"))
a$time_series<-apply( a[,c(2:18)] , 1 , paste_noNA )
> a a b c d e f g h i j k l m n o p q r s 1 1 2 10 9 6 5 2 9 8 4 NA 4 7 11 7 9 10 8 f 2 2 5 2 22 3 7 12 7 8 21 7 2 3 8 8 6 9 9 h time_series 1 2, 10, 9, 6, 5, 2, 9, 8, 4, 4, 7, 11, 7, 9, 10, 8 2 5, 2, 22, 3, 7, 12, 7, 8, 21, 7, 2, 3, 8, 8, 6, 9, 9
a<-a %>% mutate(iss=isSeasonal(time_series))
Error: Problem with `mutate()` column `iss`.
i `iss = isSeasonal(time_series)`.
x Do not know the frequency of the time series.
Run `rlang::last_error()` to see where the error occurred.
考慮上面的代碼。我試圖在列"time_series中得到數值列2到18中的值的串聯,這些值被視為一個時間序列。然后我想檢查時間序列的季節性,但是我得到了上面代碼塊末尾所述的錯誤,盡管paste_noNA函數已經將串聯轉換為時間序列。有人能幫忙嗎?
我也試過了
a<-a %>% mutate(time_series=ts(time_series,frequency=12)) %>%
mutate(iss=isSeasonal(time_series))
但我錯了
Error: Problem with `mutate()` column `time_series`.
i `time_series = ts(time_series, frequency = 12)`.
x `time_series` must be a vector, not a `ts` object.
Run `rlang::last_error()` to see where the error occurred.
在本例中,
apply
函數返回一個list對象,因為“對'FUN'的調用返回不同長度的向量”。我相信這個錯誤是因為
isSeasonal
希望您提供一個ts
對象而不是列表。我想試試