如何获得R以识别数据帧中的“时间”列?

时间:2018-08-13 10:04:02

标签: r dataframe time

我创建了一个数据框DF。

time <- c("10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00")

temperature <- c("15", "16", "17", "18", "18", "19")

DF <- data.frame(time, temperature)

当前,R将“时间”列识别为字符列表。我希望R将该列识别为时间列。

我使用了以下代码;

DF$time <- hms(DF$time)

但是它没有给我我想要的结果。

如何将“时间”列识别为时间列表?

1 个答案:

答案 0 :(得分:0)

使用软件包hms

time <- c("10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00")
temperature <- c("15", "16", "17", "18", "18", "19")

# make sure time columns is a character, not a factor
DF <- data.frame(time, temperature, stringsAsFactors = F)

library(hms)
DF$time <- as.hms(DF$time)

str(DF)
'data.frame':   6 obs. of  2 variables:
 $ time       : 'hms' num  10:00:00 11:00:00 12:00:00 13:00:00 ...
  ..- attr(*, "units")= chr "secs"
 $ temperature: chr  "15" "16" "17" "18" ...