简体   繁体   English

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

[英]Change two separate columns of month and year into one for date in R

I am really new at using R and am having difficulty changing two columns into one for time. 我在使用R方面确实很陌生,并且很难将两列更改为一列。 I have Months in 1-12 and years in 1999 etc. format. 我有1-12个月和1999年等格式。 I am looking to change into one Date which R will recognise as a date. 我希望将一个日期更改为R将识别为一个日期。 I tried using strptime but as I do not have a day this caused it to automatically use the current date instead - as date of the month isn't specified does this matter? 我尝试使用strptime,但是由于没有一天,这导致它自动使用当前日期-由于未指定月份日期,这有关系吗?

Thanks in advance 提前致谢

> 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

Not beautiful, but it works. 不漂亮,但是可以用。 Maybe somebody else can improve it: 也许其他人可以改善它:

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)

Try this: 尝试这个:

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

or use "yearmon" class in the zoo package which uses year and month with no day needed: 或在Zoo包中使用"yearmon"类,该类使用年和月,而无需日:

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

Building on G. Grothendieck's solution you can also use. 您也可以使用G. Grothendieck的解决方案。 [Also note comment below : ISOdate gives a "POSIXct" result but a "Date" or "yearmon" result would be better here.] [还请注意以下注释: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

Alternatively, 或者,

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

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

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