简体   繁体   English

如何在R中将“year.month”格式更改为“Year-Month”格式

[英]How to change "year.month" format into "Year-Month" format in R

I have a data frame that looks like this:我有一个如下所示的数据框:

Month           GSI
1993.01     -0.57567056
1993.02     -1.15549239
1993.03     -1.00353071
1993.04     -0.10698880
1993.05     -0.31903591
1993.06      0.30361638
1993.07      1.24528915
1993.08      0.85104370
1993.09      1.24680092
1993.10      1.42521406

As you can see, the "Month" column is meant to be a date in the format "year.month".如您所见,“Month”列是“year.month”格式的日期。 I would like to reformat this column to the traditional "%Y-%m" format so that the data frame looks something more like this:我想将此列重新格式化为传统的“%Y-%m”格式,以便数据框看起来更像这样:

  Date          GSI
1993-01     -0.57567056
1993-02     -1.15549239
1993-03     -1.00353071
1993-04     -0.10698880
1993-05     -0.31903591
1993-06      0.30361638
1993-07      1.24528915
1993-08      0.85104370
1993-09      1.24680092
1993-10      1.42521406

How can I go about changing the format of this column to be recognizable as a date column?如何更改此列的格式以使其可识别为日期列? Currently, the class of the "Month" column is numeric.目前,“Month”列的类是数字。

You can use sub , with capturing groups in the regular expression:您可以使用sub ,在正则表达式中捕获组:

df$Month <- sub("^(\\d{4})\\.(\\d{2})$", "\\1-\\2", format(df$Month, 2))

df
#>      Month        GSI
#> 1  1993-01 -0.5756706
#> 2  1993-02 -1.1554924
#> 3  1993-03 -1.0035307
#> 4  1993-04 -0.1069888
#> 5  1993-05 -0.3190359
#> 6  1993-06  0.3036164
#> 7  1993-07  1.2452892
#> 8  1993-08  0.8510437
#> 9  1993-09  1.2468009
#> 10 1993-10  1.4252141

Input Data输入数据

df <- structure(list(Month = c(1993.01, 1993.02, 1993.03, 1993.04, 
1993.05, 1993.06, 1993.07, 1993.08, 1993.09, 1993.1), GSI = c(-0.57567056, 
-1.15549239, -1.00353071, -0.1069888, -0.31903591, 0.30361638, 
1.24528915, 0.8510437, 1.24680092, 1.42521406)), class = "data.frame", row.names = c(NA, 
-10L))

df
#>      Month        GSI
#> 1  1993.01 -0.5756706
#> 2  1993.02 -1.1554924
#> 3  1993.03 -1.0035307
#> 4  1993.04 -0.1069888
#> 5  1993.05 -0.3190359
#> 6  1993.06  0.3036164
#> 7  1993.07  1.2452892
#> 8  1993.08  0.8510437
#> 9  1993.09  1.2468009
#> 10 1993.10  1.4252141

Use the lubridate package.使用lubridate包。

library(dplyr)
library(lubridate)
df <- transmute(df, date = ym(Month))

# if you don't know dplyr, use:
df$date <- ym(df$Month)

Note that this solution also coerces the result into a POSIXct (date) formatted variable.请注意,此解决方案还将结果强制转换为 POSIXct(日期)格式的变量。 Transmute mutates and deletes and the month variable. Transmute 变异和删除以及月份变量。

lubridate is the gold standard package for working with date (and time) data in R. Find the cheatsheat here . lubridate是在 R 中处理日期(和时间)数据的黄金标准包。在这里找到作弊工具。

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

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