如何让 R 识别数据帧中的"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)

但它并没有给我我需要的结果。

如何让它将"时间"列识别为时间列表?

使用包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" ...

相关内容

最新更新