R:将年月数据帧转换为时间序列

时间:2020-03-03 15:11:12

标签: r dataframe time-series transformation

我的情况: 我想用R分析米德湖的水高数据。unordered_set::find作为年-月的表提供。数据作为名为“ LM”的data.frame对象导入R。

结构:

 'data.frame':  86 obs. of  13 variables:
 $ Year: int  1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 ...
 $ JAN : num  NA 86 2 12 47 48 49 63 61 50 ...
 $ FEB : num  709 908 1026 1095 1157 ...
 $ MAR : num  702 907 1031 1100 1158 ...
 $ APR : num  752 922 1045 1109 1163 ...
 $ MAY : num  807 982 1079 1134 1176 ...
 $ JUN : num  909 1016 1097 1166 1183 ...
 $ JUL : num  928 1020 1103 1174 1181 ...
 $ AUG : num  926 1024 1100 1172 1177 ...
 $ SEP : num  921 1025 1098 1174 1179 ...
 $ OCT : num  915 1023 1097 1171 1176 ...
 $ NOV : num  908 1024 1096 1169 1173 ...
 $ DEC : num  908 1024 1096 1169 1170 ...

头:

  Year     JAN     FEB     MAR     APR     MAY     JUN    JUL     AUG     SEP
1 1935     ---  708.70  701.70  752.40  806.60  909.10  928.4  925.90  920.80
2 1936  907.90  908.40  906.90  922.20  982.40 1015.50 1020.4 1024.40 1024.60
3 1937 1022.20 1026.20 1031.00 1044.60 1078.70 1096.60 1102.8 1099.60 1097.60

我的问题: 如何将该数据帧转换为时间序列(类:“ ts”)对象,其结构与众所周知的“ AirPassangers”数据集相同?

我的R版本: 3.6.3

3 个答案:

答案 0 :(得分:3)

尝试这种方法。

dat <- ts(dat[-1], start=dat[1,1], end=dat[nrow(dat),1])

str(dat)
# Time-Series [1:76, 1:12] from 1935 to 2010: NA 908 1022 1095 1165 ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:12] "JAN" "FEB" "MAR" "APR" ...

数据:

library(rvest)
dat <- html_table(read_html("https://www.usbr.gov/lc/region/g4000/hourly/mead-elv.html"), 
                  fill=TRUE, header=TRUE)[[2]]

答案 1 :(得分:2)

创建新列

library(reshape2)
mdata <- melt(LM, id="Year",variable.name = "Month")
mdata$New_Date=paste0("01-",mdata$Month,"-",mdata$Year)
mdata$New_Date=as.Date(mdata$New_Date,"%d-%b-%Y")

您现在可以将其转换为ts对象。

答案 2 :(得分:2)

您还可以尝试使用新的软件包tsibble,该软件包非常简单地创建了 tidy 时间序列。尽管这需要长格式。在此处了解更多信息:https://tsibble.tidyverts.org/

library(rvest)
#> Loading required package: xml2
library(tidyverse)
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following object is masked from 'package:dplyr':
#> 
#>     id

dat <- html_table(read_html("https://www.usbr.gov/lc/region/g4000/hourly/mead-elv.html"), 
                                                                        fill=TRUE, header=TRUE)[[2]]

dat %>% 
    mutate(JAN = as.numeric(JAN)) %>% 
    pivot_longer(cols = JAN:DEC, names_to = "month", values_to = "elev") %>% 
    mutate(yearmon = paste0("01/",month,"/",Year) %>%
                                    as.Date(format = "%d/%b/%Y") %>%
                                    tsibble::yearmonth()) %>% 
    select(yearmon, elev) %>% 
    as_tsibble()
#> Warning: NAs introduced by coercion
#> Using `yearmon` as index variable.
#> # A tsibble: 912 x 2 [1M]
#>     yearmon  elev
#>       <mth> <dbl>
#>  1 1935 Jan   NA 
#>  2 1935 Feb  709.
#>  3 1935 Mar  702.
#>  4 1935 Apr  752.
#>  5 1935 May  807.
#>  6 1935 Jun  909.
#>  7 1935 Jul  928.
#>  8 1935 Aug  926.
#>  9 1935 Sep  921.
#> 10 1935 Oct  915.
#> # … with 902 more rows

reprex package(v0.3.0)于2020-03-03创建