簡體   English   中英

R:從每月時間序列中提取單月數據

[英]R: Extract single month data from a monthly time-series

問題

我有一個 1993 年 1 月至 2019 年 12 月期間的每月時間序列(下面代碼中的對象axts ),我想創建一個包含單月所有數據的時間序列,例如 2 月。

細節

這是它的結構:

str(axts)
An ‘xts’ object on Jan 1993/Dec 2019 containing:
  Data: num [1:324, 1] -28.3 -22.8 -67.1 -122.5 -72.7 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "SSH"
  Indexed by objects of class: [yearmon] TZ: UTC
  xts Attributes:  
 NULL

這是它的創作:

axts<-structure(c(-28.3, -22.8, -67.1, -122.5, -72.7, -102.6, 49.3, 107.2,
            62.6, 98.8, 51.6, -12.8, -36.5, -22.6, -53.6, -16.3, -83.6,
            -127.5, -40.2, -80.8, -71.4, -89.6, -78, -24.6, -41.8, -79.2,
            -88.9, -111.3, -127.6, -97.6, -41.6, -6.3, -8, -15.8, -21.6, 
            1.5, 33.4, 71.5, 36, -18.8, 78.5, -20.4, 111.1, 93.5, 142.5,
            110.4, 60, 78.6, 72.6, -5.2, -65.7, -102.1, -61, 109.8, 69.8,
            67.7, 47.6, 64.7, 52.7, -20, -58, 41.7, 5.8, -3.1, 120.1, 180.3,
            149.4, 213.7, 166.4, 78.5, 65.6, 12.1, -2.7, -46.1, -27.2, 
            72.8, 11.3, 46.4, 50.3, 40, 78.5, 154.8, 178.1, 99.9, 66.5,
            -47.1, -76.7, -6.9, -23.6, 113.7, 37.6, 120.6, 136.9, 116.1, 
            79.6, 33.3, 66.8, -5.3, 8.3, 95, 81.7, 82.9, 118.6, 140, 139.8,
            126.2, 157.6, 95.1, 95.7, 26.8, 116.6, 77.3, 106.7, -53.3, -104.4,
            -88.1, -58.9, -17.8, 159.1, 179.8, 27.1, -88.5, -84.2, -34.3, 61,
            102, 133.1, 162, 130, 124.8, 133.7, 109.6, 21.9, 123, 113.5, 102.4,
            117.2, 24.9, 45.6, 71.2, 163.8, 118, 54.2, 87.4, 97.9, 127.2, 11.6,
            -84.1, 15.1, 5.9, 71, 140.6, 47.4, 26.4, 36.2, 5.4, -5.4, -46.8, -25.3, 
            -79.4, -45.8, 40.1, -52.8, -95.5, -56.5, 175.6, 140.6, -10.5, -77.7, -26.8,
            73.6, 13.4, 15.4, 128.1, 129.2, 50.6, 7.5, -27.6, 19.2, 61, 84.3, -31.2,
            -19.1, 31.8, 22.4, -31, -93.9, -25.8, -4.1, 87.3, -12.8, -125.8, -58.1, 
            74.5, 41, -141.6, -142.1, -92.9, 25.6, 50.8, 139.3, 126.1, -10.4, 45.1, 
            92.2, 47.4, -1.6, -46.9, 69.4, -52.3, 18.2, 194.8, 157.6, 37.3, 28.3, -18, 
            -46.8, -95.8, -146, -172.4, -14.7, -33.4, -46.4, 5.9, 146.3, 124.5, 82.8,
            -5.9, -124.7, -108.4, -66.3, -61.8, -62.6, 29.6, 38.5, 8.3, 158.4, 80.2, 10.5,
            27.1, 21.2, 21.2, 121.8, 94.3, -86.3, -49.4, 178.4, 100.3, 68.6, 106.5, 72.1, 58.6, 
            -42.5, -74.6, -85.3, -100.1, -122.5, -125.7, -109.3, -100.6, -17, 2.8, -58, -32.4, 
            -63.2, 28.4, -99.8, -198.3, -103, 36.3, 47.5, 85.4, 124.9, 61.6, -150.2, -174.6,
            -182.3, -69.3, -48.8, -10.3, 82.3, 13.9, -40, 64.1, 126.1, 15.5, 49.8, 12.3, -105,
            -23.9, -51.7, -25.6, -140.1, -73.1, -66.1, -57, -20.1, -50.1, 25.3, -67.9, -47.8,
            6.3, 13.2, 48.1, -50.8, 26.5, -58.4, -70.6, 81.2, 41.9, -32.6, 25.1, 118.7, 82.6, 
            62.6, 13.9, -149.5, 8, -32.8, 70.8, 96.9, 146.7, 106.2, -21.3), 
          .Dim = c(324L, 1L), index = structure(c(725846400, 728524800, 730944000, 733622400, 736214400, 
                                                  738892800, 741484800, 744163200, 746841600, 749433600, 
                                                  752112000, 754704000, 757382400, 760060800, 762480000, 
                                                  765158400, 767750400, 770428800, 773020800, 775699200,
                                                  778377600, 780969600, 783648000, 786240000, 788918400, 791596800, 
                                                  794016000, 796694400, 799286400, 801964800, 804556800, 807235200, 
                                                  809913600, 812505600, 815184000, 817776000, 820454400, 823132800, 
                                                  825638400, 828316800, 830908800, 833587200, 836179200, 838857600, 
                                                  841536000, 844128000, 846806400, 849398400, 852076800, 854755200, 
                                                  857174400, 859852800, 862444800, 865123200, 867715200, 870393600,
                                                  873072000, 875664000, 878342400, 880934400, 883612800, 886291200, 
                                                  888710400, 891388800, 893980800, 896659200, 899251200, 901929600, 
                                                  904608000, 907200000, 909878400, 912470400, 915148800, 917827200,
                                                  920246400, 922924800, 925516800, 928195200, 930787200, 933465600,
                                                  936144000, 938736000, 941414400, 944006400, 946684800, 949363200,
                                                  951868800, 954547200, 957139200, 959817600, 962409600, 965088000, 
                                                  967766400, 970358400, 973036800, 975628800, 978307200, 980985600, 
                                                  983404800, 986083200, 988675200, 991353600, 993945600, 996624000,
                                                  999302400, 1001894400, 1004572800, 1007164800, 1009843200, 1012521600, 
                                                  1014940800, 1017619200, 1020211200, 1022889600, 1025481600, 1028160000, 
                                                  1030838400, 1033430400, 1036108800, 1038700800, 1041379200, 1044057600,
                                                  1046476800, 1049155200, 1051747200, 1054425600, 1057017600, 1059696000,
                                                  1062374400, 1064966400, 1067644800, 1070236800, 1072915200, 1075593600,
                                                  1078099200, 1080777600, 1083369600, 1086048000, 1088640000, 1091318400,
                                                  1093996800, 1096588800, 1099267200, 1101859200, 1104537600, 1107216000,
                                                  1109635200, 1112313600, 1114905600, 1117584000, 1120176000, 1122854400,
                                                  1125532800, 1128124800, 1130803200, 1133395200, 1136073600, 1138752000, 
                                                  1141171200, 1143849600, 1146441600, 1149120000, 1151712000, 1154390400, 
                                                  1157068800, 1159660800, 1162339200, 1164931200, 1167609600, 1170288000, 
                                                  1172707200, 1175385600, 1177977600, 1180656000, 1183248000, 1185926400, 
                                                  1188604800, 1191196800, 1193875200, 1196467200, 1199145600, 1201824000, 
                                                  1204329600, 1207008000, 1209600000, 1212278400, 1214870400, 1217548800, 
                                                  1220227200, 1222819200, 1225497600, 1228089600, 1230768000, 1233446400,
                                                  1235865600, 1238544000, 1241136000, 1243814400, 1246406400, 1249084800, 
                                                  1251763200, 1254355200, 1257033600, 1259625600, 1262304000, 1264982400, 
                                                  1267401600, 1270080000, 1272672000, 1275350400, 1277942400, 1280620800, 
                                                  1283299200, 1285891200, 1288569600, 1291161600, 1293840000, 1296518400, 
                                                  1298937600, 1301616000, 1304208000, 1306886400, 1309478400, 1312156800, 
                                                  1314835200, 1317427200, 1320105600, 1322697600, 1325376000, 1328054400, 
                                                  1330560000, 1333238400, 1335830400, 1338508800, 1341100800, 1343779200, 
                                                  1346457600, 1349049600, 1351728000, 1354320000, 1356998400, 1359676800, 
                                                  1362096000, 1364774400, 1367366400, 1370044800, 1372636800, 1375315200, 
                                                  1377993600, 1380585600, 1383264000, 1385856000, 1388534400, 1391212800, 
                                                  1393632000, 1396310400, 1398902400, 1401580800, 1404172800, 1406851200, 
                                                  1409529600, 1412121600, 1414800000, 1417392000, 1420070400, 1422748800, 
                                                  1425168000, 1427846400, 1430438400, 1433116800, 1435708800, 1438387200, 
                                                  1441065600, 1443657600, 1446336000, 1448928000, 1451606400, 1454284800, 
                                                  1456790400, 1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 
                                                  1472688000, 1475280000, 1477958400, 1480550400, 1483228800, 1485907200, 
                                                  1488326400, 1491004800, 1493596800, 1496275200, 1498867200, 1501545600, 
                                                  1504224000, 1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 
                                                  1519862400, 1522540800, 1525132800, 1527811200, 1530403200, 1533081600, 
                                                  1535760000, 1538352000, 1541030400, 1543622400, 1546300800, 1548979200, 
                                                  1551398400, 1554076800, 1556668800, 1559347200, 1561939200, 1564617600, 
                                                  1567296000, 1569888000, 1572566400, 1575158400), 
                                                tzone = "UTC", tclass = "yearmon", tformat = "%m/%Y"), class = c("xts", "zoo"), .Dimnames = list(NULL, "SSH"))
          

我編輯了您的dput ,因為其中有一些小錯誤,但現在應該很好。 我對您的數據進行了一些轉換,使其僅從每年的特定月份獲取值; 在這種情況下,二月。

#do some transformations first
axts_df<-as.data.frame(axts)  
library(data.table)
library(lubridate)
axts_df<-setDT(axts_df, keep.rownames = T)[]
axts_df$MonthYear<-my(axts_df$rn)
axts_df$Month<-month(axts_df$MonthYear)

#for February only:-
library(dplyr)
axts_df_feb<-axts_df%>%
  filter(Month==2)

#convert back into time series

axts_df_feb$rn<-NULL
axts_df_feb$Month<-NULL

axts_df_feb_ts<-ts(axts_df_feb$SSH)
plot(axts_df_feb_ts)


下面的 plot 為您提供每年 2 月的值。

在此處輸入圖像描述

但是,在ggplot2中有一種方法可以做到這一點,你可以這樣做: -

ggplot(axts_df_feb, aes(x=MonthYear,y=SSH))+geom_line()+ggtitle("Values for every February (1993-2019)")

在此處輸入圖像描述

因為 axts 的索引是axts class 我們可以使用cycle來獲取月份數,所以對於二月(一年中的第二個月):

axts[cycle(axts) == 2]

給予:

           SSH
02/1993  -22.8
02/1994  -22.6
02/1995  -79.2
02/1996   71.5
02/1997   -5.2
02/1998   41.7
## ...snip...

如果您想要這樣做的原因是為每個月創建一個plot那么我們甚至不需要它,因為它可以簡單地使用生成 12 個並排圖的月圖來完成,每個月一個。

monthplot(as.ts(axts), ylab = "SSH")

截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM