我有这个data.frame:
df
Date A_F.t. Date.1 A_S.t. Date.2 B_F.t. Date.3 B_S.t. Date.4 C_F.t. Date.5 C_S.t.
1 1988-12-29 1.7852 27-Dec-88 1.804000 1988-12-29 0.8360505 28-Dec-88 0.836820 1988-12-29 0.007959249 28-Dec-88 0.007963
2 1988-12-30 1.8027 28-Dec-88 1.789000 1988-12-30 0.8368901 29-Dec-88 0.838574 1988-12-30 0.008019246 29-Dec-88 0.007946
3 1989-01-04 1.8001 29-Dec-88 1.789500 1989-01-03 0.8394889 30-Dec-88 0.838364 1989-01-03 0.008113590 30-Dec-88 0.007997
4 1989-01-10 1.7572 30-Dec-88 1.808500 1989-01-04 0.8371704 2-Jan-89 ND 1989-01-04 0.008019246 2-Jan-89 ND
5 1989-01-16 1.7540 2-Jan-89 ND 1989-01-05 0.8368201 3-Jan-89 0.839842 1989-01-06 0.007930843 3-Jan-89 0.008091
6 1989-01-17 1.7655 3-Jan-89 1.822500 1989-01-06 0.8380825 4-Jan-89 0.838856 1989-01-09 0.007947230 4-Jan-89 0.007997
转发$ Date,转发$ Date.2,转发$ Date.4等级为"POSIXct" "POSIXt"
转发$ Date.1,转发$ Date.3,转发$ Date.5是字符
如何仅向"POSIXct" "POSIXt"
,Forward$Date.1
和Forward$Date.3
列转到Forward$Date.5
?
当我使用as.POSIXct(as.Date(x,"%d-%b-%y"))
时Ben表示我的df是这样的:
df
Date A_F.t. Date.1 A_S.t. Date.2 B_F.t. Date.3 B_S.t. Date.4 C_F.t. Date.5 C_S.t.
1 1988-12-29 1.7852 <NA> 1.804000 1988-12-29 0.8360505 <NA> 0.836820 1988-12-29 0.007959249 <NA> 0.007963
2 1988-12-30 1.8027 <NA> 1.789000 1988-12-30 0.8368901 <NA> 0.838574 1988-12-30 0.008019246 <NA> 0.007946
3 1989-01-04 1.8001 <NA> 1.789500 1989-01-03 0.8394889 <NA> 0.838364 1989-01-03 0.008113590 <NA> 0.007997
4 1989-01-10 1.7572 <NA> 1.808500 1989-01-04 0.8371704 1989-01-01 22:00:00 ND 1989-01-04 0.008019246 1989-01-01 22:00:00 ND
5 1989-01-16 1.7540 1989-01-01 22:00:00 ND 1989-01-05 0.8368201 1989-01-02 22:00:00 0.839842 1989-01-06 0.007930843 1989-01-02 22:00:00 0.008091
6 1989-01-17 1.7655 1989-01-02 22:00:00 1.822500 1989-01-06 0.8380825 1989-01-03 22:00:00 0.838856 1989-01-09 0.007947230 1989-01-03 22:00:00 0.007997
答案 0 :(得分:1)
dd <- read.table(header=TRUE,text="
Date A_F.t. Date.1 A_S.t. Date.2 B_F.t. Date.3 B_S.t. Date.4 C_F.t. Date.5 C_S.t.
1 1988-12-29 1.7852 27-Dec-88 1.804000 1988-12-29 0.8360505 28-Dec-88 0.836820 1988-12-29 0.007959249 28-Dec-88 0.007963
2 1988-12-30 1.8027 28-Dec-88 1.789000 1988-12-30 0.8368901 29-Dec-88 0.838574 1988-12-30 0.008019246 29-Dec-88 0.007946
3 1989-01-04 1.8001 29-Dec-88 1.789500 1989-01-03 0.8394889 30-Dec-88 0.838364 1989-01-03 0.008113590 30-Dec-88 0.007997
4 1989-01-10 1.7572 30-Dec-88 1.808500 1989-01-04 0.8371704 2-Jan-89 ND 1989-01-04 0.008019246 2-Jan-89 ND
5 1989-01-16 1.7540 2-Jan-89 ND 1989-01-05 0.8368201 3-Jan-89 0.839842 1989-01-06 0.007930843 3-Jan-89 0.008091
6 1989-01-17 1.7655 3-Jan-89 1.822500 1989-01-06 0.8380825 4-Jan-89 0.838856 1989-01-09 0.007947230 4-Jan-89 0.007997")
怎么样:
cols.to.fix <- c("Date.1","Date.3","Date.5")
dd[cols.to.fix] <- lapply(dd[cols.to.fix],
function(x) as.POSIXct(as.Date(x,"%d-%b-%y")))
请注意,为了成功使用此%b
格式说明符,您必须将区域设置设置为月份缩写与数据集中的缩写相匹配的区域设置;来自strptime
(强调补充):
'%b'当前语言环境中缩写的月份名称 平台即可。 (还匹配输入的全名:在某些语言环境中 没有名字的缩写。)
类似于:
old_time <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME","C")
## do stuff
Sys.setlocale("LC_TIME",old_time) ## restore locale
应该有用......