the*_*ide 1 posix replace r posixct
我有PosixCt格式的大型日期数据框。我的目标很简单:将所有日期更改为一天-2016-05-01-同时保持所有时间相同。如果转换sample$newtime为字符,我将如何继续替换字符串(每行)中的前10个字符?
> class(sample$newtime)
[1] "POSIXct" "POSIXt" 
> head(sample)
                      newtime
1 2016-05-01 02:25:34
2 2016-05-01 02:20:23
3 2016-05-01 02:13:58
4 2016-05-01 02:10:33
5 2016-05-01 02:07:36
6 2016-05-01 02:03:01
> dim(sample)
    [1] 92020     1
> range(sample$newtime)
[1] "2015-01-01 01:04:29 MSK" "2016-06-15 12:45:03 MSK"
您可以使用包中的date函数lubridate,例如:
library(lubridate)
x = Sys.time()
y = Sys.time()
x
# [1] "2016-07-11 09:16:40 EDT"
y
# [1] "2016-07-11 09:16:45 EDT"
vec <- c(x, y)
date(vec)
# [1] "2016-07-11" "2016-07-11"
date(vec) <- "2015-01-01"           # change the date here
vec
# [1] "2015-01-01 09:16:40 EST" "2015-01-01 09:16:45 EST"