R粘贴时间差(秒、分钟等)

R粘贴时间差(秒、分钟等),r,R,在R中,我想得到保持单位的字符串中的时间(例如,如果是秒或分钟)。请参阅下面的示例代码 T1 <- Sys.time() T2 <- Sys.time() duration <- T2-T1 # Looking at duration show unit: duration time_description <- paste("it took: ", round(duration, 2), sep="", col="

在R中,我想得到保持单位的字符串中的时间(例如,如果是秒或分钟)。请参阅下面的示例代码

T1 <- Sys.time()

T2 <- Sys.time()
duration <- T2-T1

# Looking at duration show unit:
duration

time_description <- paste("it took: ", round(duration, 2), sep="", col="")

# However int time description the unit is removed
time_description


T1您可以使用
units
difftime
对象中提取单位

time_description <- sprintf('it took %.2f %s', duration, units(duration))
time_description
#[1] "it took 0.39 secs"
时间描述