简体   繁体   English

将单独的年和月列合并为单个日期列

[英]Combine separate Year and Month columns into single Date Column

I have a data frame (df) like this:我有一个这样的数据框(df):

    code     year  month
1 YYOOGG    2011    8
2 YYOOGG    2011    1
3 YYOOGG    2011    4
4 YYOOGG    2011    3
5 YYOOGG    2011    12
6 YYOOGG    2011    9

and I need to create a 4th column with the Date like this:我需要用这样的日期创建第四列:

    code     year  month  Date
1 YYOOGG    2011    8     2011-08
2 YYOOGG    2014    1     2014-01
3 YYOOGG    2016    4     2016-04
4 YYOOGG    2009    3     2009-03
5 YYOOGG    2000    12    2000-12
6 YYOOGG    2010    9     2010-09

I tried this:我试过这个:

  df$Date <- as.Date(paste(df$year, df$month, sep="-"), "%Y-%M")

but I get the following as the date:但我得到以下日期:

2011-09-09 2011-09-09

I'd use zoo::as.yearmon as follows 我将如下使用zoo::as.yearmon

df$Date <- as.yearmon(paste(df$year, df$month), "%Y %m")

It will not look like the desired output (ie 2011-01). 它看起来不像所需的输出(即2011-01)。

However, IMO this approach is better than m0h3n's because df$Date will be saved as a yearmon object rather than a string. 但是,IMO这种方法比m0h3n更好,因为df$Date将保存为yearmon对象而不是字符串。 Therefore you can be handled it like a date. 因此,您可以像日期一样处理它。 For example, if you save df$Date as a string you're going to have a hard time plotting your data over time, etc... 例如,如果您将df$Date保存为字符串,那么随着时间的推移,您将很难绘制数据等...

A date typically contains a day, otherwise it's not actually a date. 日期通常包含一天,否则它实际上不是日期。 For this reason, I'll create a character vector for the new column. 出于这个原因,我将为新列创建一个字符向量。 Using only base R, you could use sprintf() to put the two columns together, adding a zero wherever necessary on the month column ... 仅使用基数R,您可以使用sprintf()将两列放在一起,在month列的任何位置添加零值...

within(df, Date <- sprintf("%d-%02d", year, month))
#     code year month    Date
# 1 YYOOGG 2011     8 2011-08
# 2 YYOOGG 2011     1 2011-01
# 3 YYOOGG 2011     4 2011-04
# 4 YYOOGG 2011     3 2011-03
# 5 YYOOGG 2011    12 2011-12
# 6 YYOOGG 2011     9 2011-09

Or 要么

df$Date <- with(df, sprintf("%d-%02d", year, month))

You can try in this way but previous OP has a better code 您可以尝试这种方式,但以前的OP有更好的代码

data <-  data.frame(code=c("ABCF","DEFG"), year = c(2011,2012), month = c(08,12))



for(i in 1:nrow(data)){
if(nchar(data$month[i])==1){
  data$Date[i] <- paste(data$year[i],data$month[i],sep="-0")
}else{
  data$Date[i] <- paste(data$year[i],data$month[i],sep="-")
  }
}

data
  code year month    Date
1 ABCF 2011     8 2011-08
2 DEFG 2012    12 2012-12

Since you have only the month and year, and you are only interested in those, I would suggest the following.由于您只有月份和年份,并且您只对这些感兴趣,因此我建议以下内容。

df %>% 
mutate(
day = "01", #Create a day column just to get a full date format. The day will be dropped in the following step
date_yr_m = as.yearmon(paste0(year,month,day), "%Y %b") #use the zoo as.yearmon() to get the year and month only
)

This will make it possible to create a 'yearmon' column that can be arranged chronologically in case you want to plot a figure.这将使创建一个“yearmon”列成为可能,如果您想绘制图形,该列可以按时间顺序排列。

Here is a dplyr solution similar to @RichScriven这是类似于@RichScriven 的dplyr解决方案

Example data frame示例数据框

df <- data.frame(matrix(ncol = 3, nrow = 6))
colnames(df)[1:3] <- c("code","year","month")
df$code <- "YYOOGG"
df$year <- 2011
df$month <- c(8,1,4,3,12,9)

Solution解决方案

library(dplyr)

df <- df %>%
  mutate(Date = with(., sprintf("%d-%02d", year, month)))

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

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