使用chron包格式化R中的数据
目前,我正在尝试将字符串转换为时间格式。使用chron包格式化R中的数据,r,chron,R,Chron,目前,我正在尝试将字符串转换为时间格式。 e、 g.我的字符串如下所示:time您的代码有效。如果选中“class()”则为“times”。但是,如果需要其他方法,请尝试: time <- '12:00:00' newtime<-as.POSIXlt(time, format = "%H:%M:%S") # The whole date with time t <- strftime(newtime, format="%H:%M:%S") # To extract the ti
e、 g.我的字符串如下所示:
time您的代码有效。如果选中“class()”则为“times”。但是,如果需要其他方法,请尝试:
time <- '12:00:00'
newtime<-as.POSIXlt(time, format = "%H:%M:%S") # The whole date with time
t <- strftime(newtime, format="%H:%M:%S") # To extract the time part
t
#[1] "12:00:00"
当我使用你的代码时,它正确地显示为12:00:00问题是什么,你能更新你的问题吗?不,它不起作用。因此,漏洞故事-我从html导入中获取时间对象,但在使用times()函数之前,我使用as.character(time)转换了对象,并且在dataframe中,对象显示为“12:00”。在执行as.characterther(time)之前,类(time)
是什么
?它被列为因子-但在data.frame中显示为普通字符串。我有两个想法:(1)如果可能的话,在阅读html时尝试编写stringsafactors=FALSE
。(2) 否则,问题可能是您试图对整个data.frame执行as.character
。按列尝试。告诉我问题是否解决了。干杯对于您的第二个想法,我使用了以下代码段:for(I in 1:nrow(table)){table$time[I]
time <- '12:00:00'
newtime<-as.POSIXlt(time, format = "%H:%M:%S") # The whole date with time
t <- strftime(newtime, format="%H:%M:%S") # To extract the time part
t
#[1] "12:00:00"