R中的Ts对象

如何解决R中的Ts对象

我正在尝试使用功能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)))})

使用输入df_Monthly表示它是31个数据帧的列表。这是其中之一的结构:

> str(df_MonthyMean[[1]])
'data.frame':   809 obs. of  3 variables:
 $ year                 : int  1953 1953 1953 1953 1953 1953 1953 1954 1954 1954 ...
 $ month                : int  6 7 8 9 10 11 12 1 2 3 ...
 $ MonthlyMeanStreamflow: num  25.1 32.2 26.2 11.6 13.6 ...

> dput(round(df_MonthyMean[[1]],1))
structure(list(year = c(1953,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2020),month = c(6,7,8,9,10,11,12,1,2,3,4,5,6,10),MonthlyMeanStreamflow = c(25.1,32.2,26.2,11.6,13.6,22.7,20,26.5,38.6,322.6,279.7,68.3,14.7,36.5,87.7,34.7,22.5,29.5,28.5,36.6,46,67.9,49.5,25.1,14.4,342.9,55.8,30.5,42.9,42,80.5,273.4,189,65.1,17.2,20.9,27.4,9.4,15.1,29.1,28.4,77.9,223.1,257.8,239.3,148.1,56.9,44,376.2,103.7,61.1,124.1,75.5,47.9,141.4,760.8,1872.3,649.4,85.1,31.6,53.9,154,206.5,60.2,51.2,40.5,48.5,66.6,66.5,29.7,19.2,33.2,251.5,60.5,48.9,163.5,109,205.5,182.2,1000.3,506.9,131.3,42.7,16.5,20.4,21.6,41.8,36.4,35.6,37.7,46.7,154.9,197.2,23.5,23.3,32.4,36.1,37.9,124.9,182.1,172.7,654.5,427.5,1793.3,295.2,56.3,34.2,18.2,41.7,59.5,54.4,50.1,296.9,321.1,289.2,69.2,28.8,32.1,143.2,384.6,165.1,128,60.4,90.6,407.2,111.3,37.5,117.9,296.8,92.6,57.8,322.2,344,549.7,1282.8,380.5,68.1,122.4,139.9,47.2,34.4,96.9,472.7,391.7,167.2,1383.9,1208.9,209.6,39.7,45.4,90.8,87.1,51.6,27.3,45.6,38.1,46.6,70.1,62,25.4,22.3,84.2,378,203.1,52.5,49.1,132.4,537.7,798.6,1290.8,473.8,41.9,128.3,36.9,25.8,39.2,33.1,153.5,127.2,325,876.8,222.7,46.2,27.7,64.4,134.7,37.1,55.7,54.5,50.3,52.7,219.4,315.2,128.7,25.3,24.8,85,58.5,28.1,56.5,78.7,23.1,15.6,122,86.4,564.2,200.7,151.8,75.4,158.5,43.7,32.6,24.5,25.7,65.2,1210.7,299.5,174.6,222.9,276.7,674.2,2058.2,1933,244.3,102.8,54.8,20.7,38,41.5,131.9,110,30,9.8,35,95.4,143.6,52.2,87.2,819.2,1052.4,518.3,72.7,47.8,24.3,120.6,23.2,169.4,232.8,507.4,214.1,31.4,37.3,39.1,24.2,18.8,24.7,29.9,32.7,40.8,64,150.1,50.4,18,80.6,48.2,34.3,33.4,176.5,1623.7,1001.5,35.1,27,42.5,23.7,26.4,282.7,915.1,391.6,525.6,1020.9,2252.7,800.2,57.4,21.5,23,32,92.9,1036,812,1644.8,890.7,136.9,61.8,79.4,76.2,27.2,40.7,40.4,28.3,40.2,194.2,447.2,120.8,51.8,77.1,61.5,69.6,38.3,43.2,57.2,195.2,664.1,759,337.8,30.7,100.3,75.1,48.7,173,374.9,1102.8,1707.8,1262,230.1,55.4,97.4,171.7,851.3,96.1,196.7,256.2,171.8,322.4,260.1,107.5,22.6,68,80.8,268.8,107,602.6,503.5,611,1863.1,1336.3,552.2,108.8,61.2,123.4,75.6,100.6,73.2,82.7,50.5,329.9,759.4,538.3,82,39.3,47.5,48.8,76.9,368,227.4,96.8,232.2,741.5,1341.7,411.7,69.9,76.4,39.4,39.9,37,331.1,457.2,701.1,328.1,60.1,54.6,311.3,366.2,60.9,51,47.3,71.3,389.8,126.3,14.1,35.4,45.8,49.9,108.4,84.1,23.8,49.8,60.7,61.7,59.2,201.1,308.5,286,1004.2,1432.8,394,75.8,82.2,56.7,130.3,135.2,400.3,864.7,1120.7,406.4,226.6,202.7,79.7,52.8,240,1570.8,984.8,1577,1926.7,687.2,157.3,62.2,61,45.9,57.7,458.3,242.7,79.1,40.9,182.6,50.7,505.3,346.1,249,986.1,1164.9,429.3,227.9,69,30.1,54.9,54.2,30.4,39.5,25.2,44.6,66.8,85.8,101,79.2,108.1,194.7,677.3,342.1,109.5,29.8,36.3,54.7,124.5,802,1032.1,465.5,50.6,38.2,31.2,40,43,91.1,33.8,30.9,67.3,509,120.3,41.3,40.1,35.2,44.9,43.5,34.9,44.4,27.5,219,332.3,52.6,115.6,426.8,658.7,152.7,69.5,35.3,32.3,36.2,21.3,50.8,74.4,28.6,47.4,40.3,61.3,166,629.8,413.9,102.5,31.1,29.2,121.1,67.4,41,564.9,375.6,65.6,25,127.6,358.6,1124.4,591.4,766,229.9,50.9,29.3,53,38.9,30.3,34,53.1,24.6,24.9,44.5,599.1,149.7,79.6,95.2,321.3,145.9,53.8,203.4,70.9,48.1,152.8,393.6,600.7,991.5,532,156.2,67.8,138.4,97.1,46.1,169.8,235.3,697.6,256.7,103.5,42.3,30.6,35.7,164.4,661.3,1280.2,390.9,63.8,129.6,29,36,35.8,63.9,45.2,64.9,163,96,27.8,64.1,335,33.6,63.2,27.9,31.5,86.1,153.4,481,174.5,48.6,193.5,578.7,88.5,55.3,42.6,115.3,23.6,47,194.9,136.4,131.5,66,99.4,327.8,203,72.3,43.1,178.9,145.1,168.8,149.3,374.5,126.5,88.4,557.6,281,32.9,48.3,328.5,527.2,934.2,684.3,205.6,63.3,188.7,24.4,26.9,32.5,19.5,43.9,525.9,1537.6,611.8,46.4,35.9,217.2,152.1,393.1,1191.2,383.4,33.9,24.1,25.3)),row.names = c(NA,-809L),class = "data.frame")

代码似乎正常工作,产生以下结果:

> 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 Slope应用于数据时遇到以下错误:

> 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.slop仅在整个周期内有效。

这可以按预期工作:

trend::sea.sens.slope(window(ts_MonthlyMean[[1]],end = c(2020,5)))
#> [1] 0.01801948

您的数据由68年零5个月组成。您只能在68年内使用sea.sens.slope。这就是为什么我提取了window的数据。


看到2021年的原因:

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

这仅仅是因为startend点在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

如果您想不对它进行四舍五入:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res