从R中的日期中提取月份和年份

时间:2020-12-15 19:44:27

I have tried a number of methods to no avail. I have data in terms of a date (YYYY-MM-DD) and am trying to get in terms of just the month and year, such as: MM-YYYY or YYYY-MM.

我尝试过多种方法无济于事。我有一个日期(YYYY-MM-DD)的数据,我试图得到的只是月份和年份,例如:MM-YYYY或YYYY-MM。

Ultimately, I would like it to look like this:

最终,我希望它看起来像这样:

ID    Date         Month_Yr
1     2004-02-06   2004-02
2     2006-03-14   2006-03
3     2007-07-16   2007-07
...   ...          ...

I am doing this in hopes of plotting money earned on average in a month, from a number of orders, over a period of time. Any help, or a push in the right direction would be much appreciated.

我这样做是希望在一段时间内从一些订单中平均赚取一个月赚来的钱。任何帮助,或正确的方向推动将非常感激。

2 个解决方案

#1


44  

This will add a new column to your data.frame with the specified format.

这将为您的data.frame添加一个具有指定格式的新列。

df$Month_Yr <- format(as.Date(df$Date), "%Y-%m")

df
#>   ID       Date Month_Yr
#> 1  1 2004-02-06  2004-02
#> 2  2 2006-03-14  2006-03
#> 3  3 2007-07-16  2007-07

# your data sample
  df <- data.frame( ID=1:3,Date = c("2004-02-06" , "2006-03-14" , "2007-07-16") )

a simple example:

一个简单的例子:

dates <- "2004-02-06"

format(as.Date(dates), "%Y-%m")
> "2004-02"

side note: the data.table approach can be quite faster in case you're working with a big dataset.

注意:如果您正在使用大型数据集,data.table方法可以更快。

library(data.table)
setDT(df)[, Month_Yr := format(as.Date(Date), "%Y-%m") ]

#2


3  

Use substring?

使用子串?

d = "2004-02-06"
substr(d,0,7)
>"2004-02"

#1


44  

This will add a new column to your data.frame with the specified format.

这将为您的data.frame添加一个具有指定格式的新列。

df$Month_Yr <- format(as.Date(df$Date), "%Y-%m")

df
#>   ID       Date Month_Yr
#> 1  1 2004-02-06  2004-02
#> 2  2 2006-03-14  2006-03
#> 3  3 2007-07-16  2007-07

# your data sample
  df <- data.frame( ID=1:3,Date = c("2004-02-06" , "2006-03-14" , "2007-07-16") )

a simple example:

一个简单的例子:

dates <- "2004-02-06"

format(as.Date(dates), "%Y-%m")
> "2004-02"

side note: the data.table approach can be quite faster in case you're working with a big dataset.

注意:如果您正在使用大型数据集,data.table方法可以更快。

library(data.table)
setDT(df)[, Month_Yr := format(as.Date(Date), "%Y-%m") ]

#2


3  

Use substring?

使用子串?

d = "2004-02-06"
substr(d,0,7)
>"2004-02"