【问题标题】:Change two separate columns of month and year into one for date in R在R中将两个单独的月份和年份列更改为一个日期
【发布时间】:2013-04-09 15:18:25
【问题描述】:

我对使用 R 非常陌生,并且很难将两列变为一列。我有 1-12 月份和 1999 年等格式的年份。我希望更改为 R 将识别为日期的日期。我尝试使用 strptime,但由于我没有一天,这导致它自动使用当前日期 - 因为没有指定月份的日期,这有关系吗?

提前致谢

> head(tthm.data)
WSZ_Code Treatment_Code Year Month TTHM CL2_FREE    BrO3 Colour  PH  TURB
1         2              3 1996     1 30.7     0.35 0.00030   0.75 7.4 0.055
2         6              1 1996     2 24.8     0.25 0.00055   0.75 6.9 0.200
3         7              4 1996     2 60.4     0.05 0.00055   0.75 7.1 0.055
6         5              2 1996     3 40.3     0.15 0.00140   2.00 7.7 0.055
11        4              1 1996     3 46.5     0.25 0.00055   1.90 7.4 0.150
14        2              3 1996     3 28.4     0.25 0.00055   1.80 7.4 0.055

【问题讨论】:

  • 欢迎来到 Stack Overflow!请在此处为好人添加可重复的样本以帮助您。见stackoverflow.com/questions/5963269/…
  • 你能确认YearMonth是否交换了吗?
  • 对不起换了什么?它们是完全分开的......我认为@e4e5f4

标签: r date


【解决方案1】:

试试这个:

transform(tthm.data, Date = as.Date(paste(Year, Month, 1, sep = "-")))

或使用 zoo 包中的 "yearmon" 类,该类使用年和月,不需要日期:

library(zoo)
transform(tthm.data, Yearmon = as.yearmon(paste(Year, Month, sep = "-")))

【讨论】:

  • 太棒了-两者都有效,将使用动物园版本,看看我的进展如何。谢谢!
【解决方案2】:

不漂亮,但它有效。也许其他人可以改进它:

x<-c(1:12)
y<-c(1999:2010)
df <- data.frame(x,y)
df$z <- ifelse(x>=10,paste0("01-",x,"-",y),paste0("01-0",x,"-",y))

df$q <- strptime(df$z,"%d-%m-%Y")
class(df$q)

【讨论】:

  • 我如何将它合并到我当前的数据集中作为一个新列?
  • 带有$ 符号。这就是我对z 所做的事情。
  • 它没有将它链接到每个样本......它只是给了我这个:(df$q) [1] "1999-01-01" "2000-02-01" "2001-03-01" "2002-04-01" "2003-05-01" "2004-06-01" "2005-07-01" "2006-08-01" "2007-09-01" "2008-10-01" "2009-11-01" "2010-12-01"
  • 是的,只需输入df。如果你这样做了df$q,当然只会显示q
【解决方案3】:

基于 G. Grothendieck 的解决方案,您也可以使用。 [还请注意下面的评论:ISOdate 给出“POSIXct”结果,但“日期”或“yearmon”结果在这里会更好。]

tthm.data
##    WSZ_Code Treatment_Code Year Month TTHM CL2_FREE    BrO3 Colour  PH  TURB
## 1         2              3 1996     1 30.7     0.35 0.00030   0.75 7.4 0.055
## 2         6              1 1996     2 24.8     0.25 0.00055   0.75 6.9 0.200
## 3         7              4 1996     2 60.4     0.05 0.00055   0.75 7.1 0.055
## 6         5              2 1996     3 40.3     0.15 0.00140   2.00 7.7 0.055
## 11        4              1 1996     3 46.5     0.25 0.00055   1.90 7.4 0.150
## 14        2              3 1996     3 28.4     0.25 0.00055   1.80 7.4 0.055


transform(tthm.data, Date = ISOdate(Year, Month, 1))
##    WSZ_Code Treatment_Code Year Month TTHM CL2_FREE    BrO3 Colour  PH  TURB                Date
## 1         2              3 1996     1 30.7     0.35 0.00030   0.75 7.4 0.055 1996-01-01 12:00:00
## 2         6              1 1996     2 24.8     0.25 0.00055   0.75 6.9 0.200 1996-02-01 12:00:00
## 3         7              4 1996     2 60.4     0.05 0.00055   0.75 7.1 0.055 1996-02-01 12:00:00
## 6         5              2 1996     3 40.3     0.15 0.00140   2.00 7.7 0.055 1996-03-01 12:00:00
## 11        4              1 1996     3 46.5     0.25 0.00055   1.90 7.4 0.150 1996-03-01 12:00:00
## 14        2              3 1996     3 28.4     0.25 0.00055   1.80 7.4 0.055 1996-03-01 12:00:00

或者,

library(zoo)
transform(tthm.data, Date = yearmon(Year + (Month - 1)/12))
##    WSZ_Code Treatment_Code Year Month TTHM CL2_FREE    BrO3 Colour  PH  TURB     Date
## 1         2              3 1996     1 30.7     0.35 0.00030   0.75 7.4 0.055 Jan 1996
## 2         6              1 1996     2 24.8     0.25 0.00055   0.75 6.9 0.200 Feb 1996
## 3         7              4 1996     2 60.4     0.05 0.00055   0.75 7.1 0.055 Feb 1996
## 6         5              2 1996     3 40.3     0.15 0.00140   2.00 7.7 0.055 Mar 1996
## 11        4              1 1996     3 46.5     0.25 0.00055   1.90 7.4 0.150 Mar 1996
## 14        2              3 1996     3 28.4     0.25 0.00055   1.80 7.4 0.055 Mar 1996

【讨论】:

  • 请注意,ISOdate 给出的结果是 "POSIXct",但这里的结果是 "Date""yearmon" 会更好。
猜你喜欢
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多