如何将日期时间格式“%Y-%m-%d %H:%M:%S”转换为 r 数据帧中的“%Y-%m-%d %H:%M:%S.sss” ?第二个分数
how to convert date time format "%Y-%m-%d %H:%M:%S" to "%Y-%m-%d %H:%M:%S.sss" in r dataframe? second with fraction
我很难在 R data.frame
中将日期时间格式 "%Y-%m-%d %H:%M:%S"
转换为 "%Y-%m-%d %H:%M:%S.sss"
?请注意,我想要带小数秒的秒数。
我不太确定你在问什么。您可以使用例如格式化小数秒"%OS3"
.
来自?strptime
Specific to R is ‘%OSn’, which for output gives the seconds
truncated to ‘0 <= n <= 6’ decimal places (and if ‘%OS’ is not
followed by a digit, it uses the setting of
‘getOption("digits.secs")’, or if that is unset, ‘n = 0’).
Further, for ‘strptime’ ‘%OS’ will input seconds including
fractional seconds. Note that ‘%S’ does not read fractional parts
on output.
示例:
ss <- "2018-08-22 21:30:00.5"
format(as.POSIXct(ss, format = "%Y-%m-%d %H:%M:%OS"), format = "%Y-%m-%d %H:%M:%OS3")
#[1] "2018-08-22 21:30:00.500"
或示例 data.frame
df <- data.frame(
date = c("2018-08-22 21:30:00", "2018-08-22 22:00:00", "2018-08-22 22:30:00"))
transform(df, new.date = format(
as.POSIXct(date, format = "%Y-%m-%d %H:%M:%OS"),
format = "%Y-%m-%d %H:%M:%OS3"))
# date new.date
#1 2018-08-22 21:30:00 2018-08-22 21:30:00.000
#2 2018-08-22 22:00:00 2018-08-22 22:00:00.000
#3 2018-08-22 22:30:00 2018-08-22 22:30:00.000
我很难在 R data.frame
中将日期时间格式 "%Y-%m-%d %H:%M:%S"
转换为 "%Y-%m-%d %H:%M:%S.sss"
?请注意,我想要带小数秒的秒数。
我不太确定你在问什么。您可以使用例如格式化小数秒"%OS3"
.
来自?strptime
Specific to R is ‘%OSn’, which for output gives the seconds truncated to ‘0 <= n <= 6’ decimal places (and if ‘%OS’ is not followed by a digit, it uses the setting of ‘getOption("digits.secs")’, or if that is unset, ‘n = 0’). Further, for ‘strptime’ ‘%OS’ will input seconds including fractional seconds. Note that ‘%S’ does not read fractional parts on output.
示例:
ss <- "2018-08-22 21:30:00.5"
format(as.POSIXct(ss, format = "%Y-%m-%d %H:%M:%OS"), format = "%Y-%m-%d %H:%M:%OS3")
#[1] "2018-08-22 21:30:00.500"
或示例 data.frame
df <- data.frame(
date = c("2018-08-22 21:30:00", "2018-08-22 22:00:00", "2018-08-22 22:30:00"))
transform(df, new.date = format(
as.POSIXct(date, format = "%Y-%m-%d %H:%M:%OS"),
format = "%Y-%m-%d %H:%M:%OS3"))
# date new.date
#1 2018-08-22 21:30:00 2018-08-22 21:30:00.000
#2 2018-08-22 22:00:00 2018-08-22 22:00:00.000
#3 2018-08-22 22:30:00 2018-08-22 22:30:00.000