如何生成一个时间序列向量(2001/01、2001/02、…)?

huangapple go评论99阅读模式
英文:

How to generate a time series vector(2001/01, 2001/02,.....)?

问题

I can help you with the translation. Here's the translated text:

我有一组包括240行数据的数据(温度数据从2001年01月到2020年12月)。

我想要生成一个类似以下的向量:

c(2001/01, 2001/02, ...... 2001/12, 2002/01, ...... 2020/12)

以便绘制时间序列图。

library(tidyverse)
df <- tibble(tem = runif(240, 1, 10),
             time = 1:240)
ggplot(df, aes(time, tem)) +
  geom_line() +
  geom_point() +
  theme_bw()

我该如何在R中实现这个目标?

英文:

I have a 240 rows data(temperature from 2001.01 to 2020.12).

I want to generate a vector just like

c(2001/01, 2001/02,...2001/12,2002/01,......2020/12)

to plot a time series figure.

library(tidyverse)
df &lt;- tibble(tem= runif(240,1,10),
             time=1:240)
ggplot(df,aes(time,tem))+
  geom_line()+
  geom_point()+
  theme_bw()

如何生成一个时间序列向量(2001/01、2001/02、…)?

How can I do it in R?

答案1

得分: 1

使用 lubridate 中的 ym。请注意,所需格式的输出是一个字符串。日期在内部始终以标准格式(数值格式)存储,例如 "2023-01-01 12:12:12",对应于 %Y-%m-%d %H:%M:%S

library(lubridate)

begin <- "2001/01"
end <- "2001/04"

format(seq.Date(ym(begin), ym(end), "month"), "%Y/%m")
[1] "2001/01" "2001/02" "2001/03" "2001/04"

如果你想要一个 ts 类型的时间序列对象:

begin <- c(2001, 1)
end <- c(2001, 4)

ts(1:4, start = begin, end = end, frequency = 12)
     Jan Feb Mar Apr
2001   1   2   3   4

编辑:
编辑显著更改了答案(几乎相当于不同的答案),仍然在此处添加它。基本上只需添加一个日期对象,然后使用 ggplot2 中的 scale_x_date 进行格式化。

library(dplyr)
library(lubridate)
library(ggplot2)

set.seed(42)

begin <- "2001/01"
end <- "2001/10"

df <- tibble(tem = runif(10, 1, 10), 
             time = seq.Date(ym(begin), ym(end), "month"))

ggplot(df, aes(time, tem)) + 
  geom_line() + 
  geom_point() + 
  theme_bw() + 
  scale_x_date(date_labels = "%Y/%m", date_breaks = "1 month")

如何生成一个时间序列向量(2001/01、2001/02、…)?

英文:

Using ym from lubridate. Note that the output in your desired format is a string. dates are internally always stored in a standardized format (numerical), e.g. "2023-01-01 12:12:12", corresponding to %Y-%m-%d %H:%M:%S

library(lubridate)

begin &lt;- &quot;2001/01&quot;

end &lt;- &quot;2001/04&quot;

format(seq.Date(ym(begin), ym(end), &quot;month&quot;), &quot;%Y/%m&quot;)
[1] &quot;2001/01&quot; &quot;2001/02&quot; &quot;2001/03&quot; &quot;2001/04&quot;

If you want a time series object of class ts

begin &lt;- c(2001, 1)

end &lt;- c(2001, 4)

ts(1:4, start = begin, end = end, frequency = 12)
     Jan Feb Mar Apr
2001   1   2   3   4

EDIT:
The edit changes the answer significantly (almost worthy a different answer), still add it here. Basically just add a date object and do the format in ggplot2 with scale_x_date.

library(dplyr)
library(lubridate)
library(ggplot2)

set.seed(42)

begin &lt;- &quot;2001/01&quot;

end &lt;- &quot;2001/10&quot;

df &lt;- tibble(tem = runif(10, 1, 10), 
             time = seq.Date(ym(begin), ym(end), &quot;month&quot;))

ggplot(df, aes(time, tem)) + 
  geom_line() + 
  geom_point() + 
  theme_bw() + 
  scale_x_date(date_labels = &quot;%Y/%m&quot;, date_breaks = &quot;1 month&quot;)

如何生成一个时间序列向量(2001/01、2001/02、…)?

答案2

得分: 0

Sure, here is the translated code part without any other content:

Well, just `plot` it maybe.

    plot(df$tmp, type='o', pch=20, xaxt='n', xlab='date', ylab='temperature', main='My Header')
    axis(1, axTicks(1)[-1], df$date[axTicks(1)])

Data:

v <- seq(as.Date("2001-01-01"), as.Date("2020-12-01"), "month")
set seed(42)
df <- data.frame(date=strftime(v, "%Y.%m"), tmp=rnorm(length(v))
英文:

Well, just plot it maybe.

plot(df$tmp, type=&#39;o&#39;, pch=20, xaxt=&#39;n&#39;, xlab=&#39;date&#39;, ylab=&#39;temperature&#39;, main=&#39;My Header&#39;)
axis(1, axTicks(1)[-1], df$date[axTicks(1)])

如何生成一个时间序列向量(2001/01、2001/02、…)?


Data:

v &lt;- seq(as.Date(&quot;2001-01-01&quot;), as.Date(&quot;2020-12-01&quot;), &quot;month&quot;)
set.seed(42)
df &lt;- data.frame(date=strftime(v, &quot;%Y.%m&quot;), tmp=rnorm(length(v)))

huangapple
  • 本文由 发表于 2023年3月15日 20:30:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定