简体   繁体   English

从 R 中的每日温度数据创建时间序列

[英]Create time series from daily temperature data in R

I have a 24 year data set (from 1/1/1980 to 31/12/2014) of daily temperature data in a data frame.我在一个数据框中有一个 24 年的数据集(从 1980 年 1 月 1 日到 2014 年 12 月 31 日)的每日温度数据。 The form of my data frame is as follows:我的数据框的形式如下:

date日期 st1 st1
1980-1-1 1980-1-1 -2.3 -2.3
1980-1-2 1980-1-2 -1.2 -1.2
1980-1-3 1980-1-3 0.8 0.8
... ... ... ...
2014-12-31 2014-12-31 4.7 4.7

( "..." stands for sequence of data from 1980 - 2014) In order to use the time series packages I need to convert the data frame in time series class. (“...”代表 1980 - 2014 年的数据序列)为了使用时间序列包,我需要转换时间序列 class 中的数据帧。 I used the following code:我使用了以下代码:

temp_ts <- ts(temp_data$st1,start = c(1980,1,1), end = c(2014,12,31),frequency = 12)

I get a complete time series, with all months in the heading of each column, and a year in each row in form of a table, as follows:我得到了一个完整的时间序列,每列的标题中包含所有月份,表格形式的每一行中包含一年,如下所示:

Jan Feb二月 Mar三月 Apr四月 May可能 June六月 .. ..
1980 1980 1.8 1.8 -1.0 -1.0 -4.8 -4.8 -5.2 -5.2 -3.1 -3.1 .. ..
1981 1981年 -5.0 -5.0 -5.2 -5.2 -3.8 -3.8 -0.5 -0.5 0.3 0.3 0.2 0.2 .. ..
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2014 2014 5.3 5.3 6.3 6.3 -1.7 -1.7 -4.3 -4.3 -6.7 -6.7 -4.7 -4.7 .. ..

( there is no "..." in my table, i just use it here to save some space) (我的表中没有“...”,我只是在这里使用它以节省一些空间)

The problem is that the data in this time series are wrong.问题是这个时间序列中的数据是错误的。 More specifically, in May the temperatures in my original data are >20 C, so it can't have as an average value negative values.更具体地说,5月份我的原始数据中的温度> 20 C,所以它不能作为平均值负值。 Same thing applies to all other months.同样的事情适用于所有其他月份。 This means that in my time series, each month column doesn't have the appropriate average value of each month but something else which is I'm not capable to identify how it was calculated.这意味着在我的时间序列中,每个月的列都没有每个月的适当平均值,但是我无法确定它是如何计算的。

I would appreciate every help in order to identify and solve this mistake by creating a time series in which each value corresponds to the average value of corresponding year and month.我希望通过创建一个时间序列来识别和解决这个错误的每一个帮助,其中每个值对应于相应年份和月份的平均值。

Convert the data to monthly data first.首先将数据转换为月度数据。 Then transform the monthly data into a ts.然后将每月的数据转换成一个ts。 Try the following command according to convert the data to monthly data:根据将数据转换为月度数据,尝试以下命令:

df %>%
group_by(month = floor_date(date, "month")) %>%
summarize(temp = mean(temp))

Save this df into a variable and then transform that into a ts.将此df保存到变量中,然后将其转换为ts。 I hope this might work for you.我希望这可能对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM