Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的Ts对象_R_Date_Time_Statistics_Time Series - Fatal编程技术网

R中的Ts对象

R中的Ts对象,r,date,time,statistics,time-series,R,Date,Time,Statistics,Time Series,我正在尝试使用函数ts和以下代码将几个站点的月径流数据转换为R中的时间序列对象: ts_MonthlyMean <- lapply(df_MonthyMean, function(x){ts(x$MonthlyMeanStreamflow, frequency=12,

我正在尝试使用函数ts和以下代码将几个站点的月径流数据转换为R中的时间序列对象:

ts_MonthlyMean <- lapply(df_MonthyMean, function(x){ts(x$MonthlyMeanStreamflow,
                                                              frequency=12,
                                                              start=c(x[1,1],x[1,2]), 
                                                              end=c(tail(x$year,1),tail(x$month,1)))})
代码似乎工作正常,产生以下结果:

> round(ts_MonthlyMean[[1]],1)
        Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec
1953                                      25.1   32.2   26.2   11.6   13.6   22.7   20.0
1954   26.5   38.6  322.6  279.7   68.3   14.7   36.5   87.7   34.7   22.5   29.5   28.5
1955   36.6   46.0   67.9   49.5   25.1   14.4   46.0  342.9   55.8   26.5   30.5   42.9
1956   42.0   80.5  273.4  189.0   65.1   17.2   20.9   27.4    9.4   15.1   29.1   28.4
1957   77.9  223.1  257.8  239.3  148.1   56.9   44.0  376.2  103.7   61.1  124.1   75.5
1958   47.9  141.4  760.8 1872.3  649.4   85.1   31.6   53.9  154.0  206.5   60.2   51.2
1959   40.5   48.5   66.6   66.5   29.7   19.2   33.2  251.5   60.5   48.9  163.5  109.0
1960  205.5  182.2 1000.3  506.9  131.3   42.7   16.5   20.4   21.6   41.8   36.4   35.6
1961   37.7   46.7  154.9  197.2   40.5   23.5   23.3   32.4   36.1   37.9  124.9  182.1
1962  172.7  654.5  427.5 1793.3  295.2   56.3   34.2   18.2   41.7   59.5   54.4   46.0
1963   50.1  296.9  321.1  289.2   69.2   28.8   32.1  143.2  384.6  165.1  128.0   60.4
1964   36.6   36.6   90.6  407.2  111.3   37.5   48.5  117.9  296.8   92.6   50.1   57.8
1965  322.2  344.0  549.7 1282.8  380.5   68.1  122.4  139.9   47.2   34.4   96.9  472.7
1966  391.7  167.2 1383.9 1208.9  209.6   39.7   45.4   90.8   87.1   51.6   27.3   45.6
1967   38.1   46.6   70.1   62.0   25.4   22.3   84.2  378.0  203.1   52.5   49.1   61.1
1968  132.4  537.7  798.6 1290.8  473.8   77.9   41.9  128.3   36.9   25.8   39.2   33.1
1969  153.5  127.2  325.0  876.8  222.7   46.2   27.7   64.4  134.7   37.1   55.7   54.5
1970   50.3   52.7  219.4  315.2  128.7   25.3   24.8   36.6   85.0   58.5   26.5   28.1
1971   44.0   56.5   78.7   45.6   23.1   15.6   28.8  122.0   86.4  564.2  200.7  203.1
1972  151.8   75.4  158.5   43.7   24.8   32.6   24.5   25.7   65.2 1210.7  299.5  174.6
1973  222.9  276.7  674.2 2058.2 1933.0  244.3  102.8   54.8   20.7   22.3   38.0   36.9
1974   41.5   34.7  131.9  110.0   30.0    9.8   22.3   75.5   35.0   95.4  143.6   48.5
1975   52.2   87.2  819.2 1052.4  518.3   72.7   47.8   24.3  120.6   23.2   25.1   27.7
1976   32.2  169.4  232.8  507.4  214.1   31.4   37.3   39.1   24.2   18.8   24.7   29.9
1977   32.7   40.8   64.0  150.1   50.4   18.0   42.7   80.6   48.2   34.3   33.4   31.4
1978   40.5  176.5 1623.7 1001.5  222.7   35.1   27.0   42.5   23.7   26.4  282.7  915.1
1979  391.6  525.6 1020.9 2252.7  800.2  239.3   57.4   62.0   21.5   23.0   32.0   24.5
1980   92.9 1036.0  812.0 1644.8  890.7  136.9   61.8   79.4   76.2   27.2   40.7   40.4
1981   28.3   40.2  194.2  447.2  120.8   36.5   51.8   77.1   61.5   69.6   38.3   43.2
1982   57.2  195.2  664.1  759.0  337.8   60.5   30.7  100.3   75.1   18.8   48.7  195.2
1983  173.0  374.9 1102.8 1707.8 1262.0  230.1   55.4   97.4  171.7  851.3   96.1  196.7
1984  256.2  171.8  322.4  260.1  107.5   22.6   25.3   68.0   80.8  268.8  107.0  602.6
1985  503.5  611.0 1863.1 1336.3  552.2  108.8   61.2  123.4   75.6  100.6   73.2   82.7
1986   50.5  329.9  759.4  538.3   82.0   39.3   54.4   47.5   48.8   76.9  368.0  227.4
1987   96.8  232.2  741.5 1341.7  411.7   69.9   39.7   76.4   39.4   39.9   97.4   37.0
1988   35.1  331.1  457.2  701.1  328.1   60.1   54.6  311.3  366.2   60.9   51.0   47.3
1989   71.3   96.9  389.8  126.3   42.7   21.6   14.1   48.7   29.5   33.1   32.2   35.4
1990   45.8   49.9  108.4   84.1   44.0   23.8   49.8   60.7   49.8   61.7   59.2  201.1
1991  308.5  286.0 1004.2 1432.8  394.0   75.8   50.5   82.2  131.3   36.9   56.7  130.3
1992  135.2  400.3  864.7 1120.7  406.4  226.6   57.8  202.7   79.7   46.2   52.8  240.0
1993 1570.8  984.8 1577.0 1926.7  687.2  157.3   62.2   61.7   61.0   45.9   49.1   50.1
1994   41.5   57.7  458.3  242.7   79.1   27.3   17.2   40.9  182.6   50.7  505.3  346.1
1995  249.0  986.1 1164.9  429.3  227.9   69.0   30.1   54.9   54.2   30.4   33.1   23.8
1996   23.0   39.5   30.1   32.0   22.5   25.2   44.6   66.8   85.8   58.5  101.0   79.2
1997  108.1  194.7  677.3  342.1  109.5   34.7   29.8   39.7   42.7   32.1   36.3   37.7
1998   54.7  124.5  802.0 1032.1  465.5   66.5   50.6   38.2   31.2   40.0   37.1   43.0
1999   38.6   39.3   31.2   91.1   33.8   30.9   67.3  509.0  120.3   41.3   38.2   40.4
2000   40.1   35.2   44.9   43.5   31.2   34.9   34.2   44.4   27.5  219.0  332.3  100.6
2001   52.6  115.6  426.8  658.7  152.7   32.0   33.4   69.5   35.3   29.7   32.3   38.3
2002   36.2   35.1   35.2   27.0   23.1   21.3   50.8   52.6   74.4   28.6   47.4   40.3
2003   61.3  166.0  629.8  413.9  102.5   31.1   29.2   44.4  121.1   30.4   67.4   41.0
2004   42.5   60.7  564.9  375.6   65.6   25.0   37.7   30.7   29.7   30.0   47.2  127.6
2005  358.6 1124.4  591.4  766.0  229.9   50.9   29.3   53.0   38.9   30.3   34.0   32.7
2006   30.9   30.4   41.3   53.1   24.6   24.9   44.5  599.1  149.7   79.6   43.5   36.4
2007   41.0   95.2  321.3  145.9   53.8   32.4   32.7  203.4   70.9   54.2   48.1  152.8
2008  393.6  600.7  991.5  532.0  156.2   50.5   67.8  138.4  296.8   97.1   39.7   46.1
2009  169.8  235.3  697.6  256.7  103.5   42.3   36.5   30.6   34.4   32.6   35.7   38.0
2010  128.3  164.4  661.3 1280.2  390.9   52.6   63.8  129.6   44.9   29.3   29.0   36.0
2011   35.8   35.4   63.9   45.2   25.3   26.5   64.9  163.0   96.0   39.7   27.8   36.3
2012   64.1  108.8  335.0  153.5   33.6   25.2   33.1   63.2   52.7   23.7   27.9   31.5
2013   86.1  153.4  481.0  174.5   48.6   25.7  203.1  193.5  578.7   88.5   55.3   67.8
2014   42.6   44.0  115.3   41.8   26.5   23.6   47.0  194.9  136.4  131.5   49.1   66.0
2015   99.4  327.8  203.0   72.3   43.1   28.1  178.9  145.1  168.8  149.3  374.5  126.5
2016   88.4  557.6  281.0   85.1   41.8   31.4   32.9   44.5   26.2   36.6   48.3  328.5
2017  527.2  934.2  684.3  205.6   63.3   27.4   66.8  188.7   30.4   31.4   22.3   24.4
2018   26.9   32.5   32.9   23.5   24.7   19.5   29.9   42.5   43.9   61.1   33.4   29.0
2019   75.5  525.9 1537.6  611.8  154.0   46.4   28.4   41.0   35.9   31.6   40.4  217.2
2020  152.1  393.1 1191.2  383.4   78.7   29.1   33.9   29.9   24.1   25.3              
> 
然而,在代码环境中,时间序列数据(对象ts)似乎从1953年到2021年,而不是到2020年

> str(ts_MonthlyMean[[1]])
 Time-Series [1:809] from 1953 to 2021: 25.1 32.2 26.2 11.6 13.6 ...
发生这种情况的原因以及我如何解决

同时,我在将季节性Sen斜率应用于数据时遇到问题,导致以下错误:

> sea.sens.slope(ts_MonthlyMean[[1]])
Error in d[, i] <- .d(dat) : 
  number of items to replace is not a multiple of replacement length
>sea.sens.slope(ts_MonthlyMean[[1]]

d[,i]中的错误问题是
sea.sens.slop
仅对完整周期有效

这与预期的效果一样:

trend::sea.sens.slope(window(ts_MonthlyMean[[1]], end = c(2020,5)))
#> [1] 0.01801948
您的数据由68年零5个月组成。您只能在68年内使用
sea.sens.slope
。这就是为什么我选择了一个
窗口


您在中看到2021的原因:

str(ts_MonthlyMean[[1]])
#> Time-Series [1:809] from 1953 to 2021: 25.1 32.2 26.2 11.6 13.6 ...
这只是因为
开始
结束
点在
str
中默认为四舍五入:

tsp(ts_MonthlyMean[[1]])
#> [1] 1953.417 2020.750   12.000

oo <- options(digits = 3) # change options the same way str does

tsp(ts_MonthlyMean[[1]])
#> [1] 1953 2021   12

options(oo) # reset options
tsp(ts_MonthlyMean[[1]]
#> [1] 1953.417 2020.750   12.000
oo[1]1953 2021 12
选项(oo)#重置选项
如果希望看到它不是四舍五入的:

str(ts_MonthlyMean[[1]])
#> Time-Series [1:809] from 1953 to 2021: 25.1 32.2 26.2 11.6 13.6 ...


# change str options
stro <- getOption("str")
stro$digits.d <- 7
oo <- options(str = stro)


str(ts_MonthlyMean[[1]])
#> Time-Series [1:809] from 1953.417 to 2020.75: 25.1 32.2 26.2 11.6 13.6 22.7 20 26.5 38.6 322.6 ...

options(oo) # reset options
str(ts_monthlyman[[1]]
#>1953年至2021年的时间序列[1:809]:25.132.226.211.613.6。。。
#更改str选项

stro你为什么要用Lappy?在您的代码中,您不需要这样做。我使用它将列表中名为df_MonthyMeana的所有数据帧转换为ts对象一个名为
df_…
的列表可能会误导您的队友。我建议你把它叫做
l\u…
str(ts_MonthlyMean[[1]])
#> Time-Series [1:809] from 1953 to 2021: 25.1 32.2 26.2 11.6 13.6 ...


# change str options
stro <- getOption("str")
stro$digits.d <- 7
oo <- options(str = stro)


str(ts_MonthlyMean[[1]])
#> Time-Series [1:809] from 1953.417 to 2020.75: 25.1 32.2 26.2 11.6 13.6 22.7 20 26.5 38.6 322.6 ...

options(oo) # reset options