将月份和年份的两个单独列更改为R中的一个日期列

时间:2013-04-09 15:18:25

标签: r date

我真的很擅长使用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

3 个答案:

答案 0 :(得分:0)

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

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)

答案 1 :(得分:0)

试试这个:

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

或在动物园套餐中使用"yearmon"课程,该课程使用年份和月份,无需任何日期:

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

答案 2 :(得分:0)

在G. Grothendieck的解决方案的基础上,你也可以使用。 [另请注意下面的注释:ISOdate给出了“POSIXct”结果,但“Date”或“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