r - 具有 R 和 ts 函数的时间序列
问题描述
我用 R 编写了一个脚本,以根据日期表示我的温度时间序列。它运作良好,但我对最后的图形表示不满意。
我的目标是选择一个时间窗口,例如从 2017 年 4 月 1 日到 2017 年 4 月 30 日,并代表我每天的温度变化(例如,4 月 30 次毕业,5 月 31 次等。 ,或日期)。
在我的代码下方,以及我试图代表我想要的第二个代码。感谢您的帮助/建议!
setwd("C:/XXX")
#Packages
library(ncdf4)
library(stringr)
library(Rcpp)
library(plyr)
library(lubridate)
library(ggplot2)
#My file
nc <-nc_open("NOAA_SST_2017_Corse.nc")
print(nc)
#Variables
lon <- ncvar_get(nc, "lon")
dim(lon)
lat <- ncvar_get(nc, "lat")
dim(lat)
time1 = ncvar_get(nc, "time")
time = as_date(time1,tz="UTC",origin = "1800-01-01")
dim(time)
#Spatial zoom
lonIdx <- which( lon >= 8.4 & lon <= 9.7)
latIdx <- which( lat >= 41.3 & lat <= 43.1)
levelIdx <- 1:length(time)
#dataframe
data_temp<-ncvar_get(nc, "sst")[lonIdx, latIdx, levelIdx]
indices <- expand.grid(lon[lonIdx], lat[latIdx], time[levelIdx])
df <- data.frame(cbind(indices, as.vector(data_temp)))
colnames(df)<-c("longitude","latitude","time","temperature")
View(df)
Final1 = df
write.table(df,"NOAA_SST_2017_Corse")
##removeNA
df2 = na.omit(df)
##daily mean
Final1 <- tapply(df2$temperature,df2$time,mean)
Final1 = as.data.frame(Final1)
View(Final1)
colnames(Final1) = "temperature"
Final2 = as.data.frame(cbind("time"=rownames(Final1),"temperature"=Final1$temperature))
Final2
View(Final2)
> head(Final2)
jour temp
1 2017-01-01 15.88318
2 2017-01-02 15.81000
3 2017-01-03 15.85045
4 2017-01-04 15.86500
5 2017-01-05 15.73000
6 2017-01-06 15.45773
> tail(Final2)
jour temp
360 2017-12-26 14.02045
361 2017-12-27 13.89318
362 2017-12-28 13.81000
363 2017-12-29 13.89091
364 2017-12-30 13.96409
365 2017-12-31 14.04682
#data?
class(Final2$temperature)
class(Final2$time)
#change variables names
names(Final2)[1]<-"jour"
names(Final2)[2]<-"temp"
#change variable
Final2$temp<-as.character(Final2$temp)
##change variable to numeric class
Final2$temp<-as.numeric(Final2$temp)
class(Final2$temp)
class(Final2$jour)
> class(Final2$temp)
[1] "numeric"
> class(Final2$jour)
[1] "factor"
#time series creation
ts.stt<-ts(data = Final2$temp, start=c(2017,04),frequency = 365.25)
#graphe
install.packages("itsmr")
library(itsmr)
windows()
plotc(ts.stt)
我在下面尝试的第二个代码
plot(Final2$jour, Final2$temp, type = "l", xaxt = "n", xlab = "",
ylab = "SST moyenne/jour", col = "royalblue3")
axis.POSIXct(1, at = seq(Final2$jour[1], tail(Final2$jour, 1), "date"),
format = "%y/%m/%d", las = 2)
Error in del/by : argument non numérique pour un opérateur binaire
De plus : Warning message:
In Ops.factor(to, if (int) as.double(from) else from) :
‘-’ not meaningful for factors
解决方案
推荐阅读
- c++ - 使用 range-v3 构建无限范围
- python - 如何在 python pyvista 中读取 dem 文件
- c++ - c ++:将父类转换为子类并调用子类的函数
- javascript - `yarn add` 和 `npm install --save` 都不会将包安装到本地项目
- javascript - Firebase SDK 身份验证 reauthenticateWithCredential 有什么意义?
- spring-boot - 弹簧配置文件未加载某些属性?
- ios - 为什么通知服务扩展没有在 iOS 14 上触发?
- scala - 将 Scala 3.0 SDK 添加到 IntelliJ
- python - 在 python 中使用 pymodbus 读取离散输入
- ios - iOS - 仅使用 Opus 编解码器的 WebRTC 单向音频