简体   繁体   中英

How can I create time series in R?

I would like to create time series in R, but I have problems with it. I have wind speed data from 30.07. to 02.09. on an hourly resolution. On the x-axis should be the date and time and on the y-axis wind speed. I tried this script, but unfortunately it is not working. Can anyone please help me? This is my code:

options(stringsAsFactors = FALSE)



 input1 <- "C:\\Users\\wind_speed.csv"

    wind_speed <- read.csv(input1, sep=";")

    dput(wind_speed)

    library(ggplot2)

    wind_speed$dateasdate <-gsub("\\.", "-", wind_speed$date)

    wind_speed$dateasdate <- dmy(wind_speed$dateasdate)

    wind_speed$date = as.Date(wind_speed$date, format = "%d.%m%.%y")

    time <- strptime(wind_speed$time, format = "%H:%M:%S")

    wind_speed$x <- paste(wind_speed$date,wind_speed$time)

    timee2<- strptime(wind_speed$x, format= "%d.%m.%y %H:%M")

    p <-ggplot(wind_speed, aes(x=x, y=speed)) + geom_point(stat="identity") + 
      geom_line(linetype="dashed") 

print(p)

[The time series should look like the image, but with date and time on the x-axis][2]

My data looks like this

This is a sample of my data:

head(wind_speed)
        date     time speed
1 27.07.2018 01:00:00   1.3
2 27.07.2018 02:00:00   0.8
3 27.07.2018 03:00:00   1.2
4 27.07.2018 04:00:00   0.6
5 27.07.2018 05:00:00   0.8
6 27.07.2018 06:00:00   1.8

Use -

library(xts)
a <- xts(df$speed, order.by=as.POSIXct(paste(df$date,df$time), format="%d.%m.%Y %H:%M:%S"))
plot.xts(a)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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