基于 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