如何从apply.monthly改变给定的一天

时间:2019-05-27 09:10:32

标签: r

在我的两个数据集(xts)上使用apply.monthly函数将每月给我两个不同的日子,例如:2018-10-15(data1)和2018-10-01(data2)。

有什么简单的方法可以说每个月的值都应该在每月的第一天出现?

我的目标是将两个数据集绑定在一起,如果并非所有日期都相等,这将不起作用。

gw:


1966-11-01 76.26
1966-11-02    NA
1966-11-03    NA
1966-11-04    NA
1966-11-05    NA
1966-11-06    NA
1966-11-07    NA
1966-11-08    NA
1966-11-09    NA
1966-11-10    NA
1966-11-11    NA
1966-11-12    NA
1966-11-13    NA
1966-11-14    NA
1966-11-15 75.66
1966-11-16    NA
1966-11-17    NA
1966-11-18    NA
1966-11-19    NA
1966-11-20    NA
1966-11-21    NA
1966-11-22    NA
1966-11-23    NA
1966-11-24    NA
1966-11-25    NA
1966-11-26    NA
1966-11-27    NA
1966-11-28    NA
1966-11-29    NA
1966-11-30    NA
1966-12-01 75.56

gwn(首先指定为df)

Time gwn
69  Jul 1966  -4.018199
70  Aug 1966  -4.986970
71  Sep 1966  -0.238788
72  Okt 1966   3.594059
73  Nov 1966   5.512350
74  Dez 1966  21.589480
````


I converted it to xts like:

````
1966-07-01  -4.018199
1966-08-01  -4.986970
1966-09-01  -0.238788
1966-10-01   3.594059
1966-11-01   5.512350
1966-12-01  21.589480

````


the code I used so far:
````
workspace1<-...
workspace2<-...
workspace3<-...
files1<-list.files(workspace1,"csv", recursive = T)
files2<-list.files(workspace2,"csv", recursive = T)
files3<-list.files(workspace1,"txt", recursive = T)
gw<-list()
gwn<-list()

for (i in 1:length(files1)){
    gw[[i]]<-ReadTabletoXTS(paste(workspace1,files1[i],sep="/"), header = T,   sep = ",", format = "%Y-%m-%d")
    gw[[i]]<-CutTimeSeries(gw[[i]], start = "1960-01-01 00:00", end = "2018-10-31 00:00")
    gw[[i]]<-na.omit(gw[[i]])
    gw[[i]]$monthly<-apply.monthly(gw[[i]]$WSP, FUN = mean)

    #converting from df to xts
    gwn[[i]]<-read.csv(paste(workspace2,files2[i],sep="/"), header = T, sep = ",")
    gwn[[i]][,c(3,4,5,6)]<-NULL
    gwn[[i]]$Time<-as.yearmon(gwn[[i]]$Time)
    gwn[[i]]$Time<-as.Date(gwn[[i]]$Time)
    dateinamen=strsplit(files2[i], split = "[.]")[[1]][1]
    dateinamen_csv<-paste(dateinamen, "txt", sep = ".")
    speicherpfad_inkl_namen_csv<-paste(workspace2,dateinamen_csv, sep = "/")
    write.csv(gwn[[i]], speicherpfad_inkl_namen_csv, row.names = F)
    gwn[[i]]<-ReadTabletoXTS(paste(workspace2,files3[i],sep="/"), header = T, sep = ",", format = "%Y-%m-%d")
    gwn[[i]]<-apply.monthly(gwn[[i]], mean)    
    }

````

0 个答案:

没有答案