简体   繁体   English

如何在 R 中创建新的日期(月、年)数据

[英]how to create a new date (month, year) data in R

I have a very simple question and I hope you can help me.我有一个非常简单的问题,希望你能帮助我。 I have a dataset with monthly temperatures from 1958 to 2020. This gives me a total of 756 observations, which matches with the amount of months.我有一个数据集,其中包含 1958 年到 2020 年的月度温度。这给了我总共 756 个观测值,与月数相匹配。 This is the only column I have, and I would like to add a column with the date in format month-year, starting from 01-1958 in the first observation, following 02-1958, 03-1958...... 12-2020.这是我唯一的一列,我想在第一次观察中添加一个日期格式为月-年的列,从 01-1958 开始,继 02-1958、03-1958 之后...... 12 -2020 年。

Any ideas?有任何想法吗?

Thank you very much!非常感谢!

Two things:两件事情:

  1. I think a Date object would be much better (there is no Month object), since it has natural number-like properties that allows you to find differences, plot without bias, etc. Note that stored this way, every other representation can be derived trivially for reports/renders.我认为Date object 会好得多(没有Month对象),因为它具有类似自然数的属性,可以让您找到差异,plot 没有偏差等。请注意,以这种方式存储,可以导出所有其他表示对于报告/渲染来说微不足道。

  2. Even if you must go with a string, I suggest putting year first so that sorting works as expected.即使您必须使用字符串 go ,我建议将年份放在首位,以便按预期进行排序。

You offered no data, so I'll make something up:你没有提供数据,所以我会弥补:

mydata <- data.frame(val = 1:756)
mydata$date <- seq(as.Date("1958-01-01"), length.out=756, by="month")
mydata$ym_chr <- format(mydata$date, format = "%Y-%m")
mydata$my_chr <- format(mydata$date, format = "%m-%Y")
mydata[c(1:5, 752:756),]
#     val       date  ym_chr  my_chr
# 1     1 1958-01-01 1958-01 01-1958
# 2     2 1958-02-01 1958-02 02-1958
# 3     3 1958-03-01 1958-03 03-1958
# 4     4 1958-04-01 1958-04 04-1958
# 5     5 1958-05-01 1958-05 05-1958
# 752 752 2020-08-01 2020-08 08-2020
# 753 753 2020-09-01 2020-09 09-2020
# 754 754 2020-10-01 2020-10 10-2020
# 755 755 2020-11-01 2020-11 11-2020
# 756 756 2020-12-01 2020-12 12-2020

As a quick demonstrating that we are looking at exactly (no more, no fewer) than one month per year, all months, all years, here's a quick table:作为一个快速演示,我们正在查看(不多,不少于)每年一个月,所有月份,所有年份,这里有一个快速表格:

table(year=gsub(".*-", "", mydata$my_chr), month=gsub("-.*", "", mydata$my_chr))
#       month
# year   01 02 03 04 05 06 07 08 09 10 11 12
#   1958  1  1  1  1  1  1  1  1  1  1  1  1
#   1959  1  1  1  1  1  1  1  1  1  1  1  1
#   1960  1  1  1  1  1  1  1  1  1  1  1  1
# ...
#   2018  1  1  1  1  1  1  1  1  1  1  1  1
#   2019  1  1  1  1  1  1  1  1  1  1  1  1
#   2020  1  1  1  1  1  1  1  1  1  1  1  1

All snipped rows are identical in all but the year , ie, all 1 s.除了year之外,所有剪断的行都是相同的,即所有1 s。 The sum(.) of this is 756. (Just checking since I wanted to make sure I was doing it right.)这个sum(.)是 756。(只是检查,因为我想确保我做对了。)

Lastly, to highlight my comment about sorting, here are some examples premised on the knowledge that val is incrementing from 1 .最后,为了突出我对排序的评论,这里有一些示例,前提是知道val1递增。

head(mydata[order(mydata$ym_chr),])
#   val       date  ym_chr  my_chr
# 1   1 1958-01-01 1958-01 01-1958
# 2   2 1958-02-01 1958-02 02-1958
# 3   3 1958-03-01 1958-03 03-1958
# 4   4 1958-04-01 1958-04 04-1958
# 5   5 1958-05-01 1958-05 05-1958
# 6   6 1958-06-01 1958-06 06-1958

head(mydata[order(mydata$my_chr),])
#    val       date  ym_chr  my_chr
# 1    1 1958-01-01 1958-01 01-1958
# 13  13 1959-01-01 1959-01 01-1959
# 25  25 1960-01-01 1960-01 01-1960
# 37  37 1961-01-01 1961-01 01-1961
# 49  49 1962-01-01 1962-01 01-1962
# 61  61 1963-01-01 1963-01 01-1963

If being able to sort by date is important, than I suggest it will be much simpler to use either $date or the string $ym_chr .如果能够按日期排序很重要,那么我建议使用$date或字符串$ym_chr会简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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