【问题标题】:How do I get R to recognise a "time" column within a dataframe?如何让 R 识别数据框中的“时间”列?
【发布时间】:2018-08-13 10:04:02
【问题描述】:

我创建了一个数据框,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)

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

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

【问题讨论】:

  • 您需要/期望什么结果?

标签: r dataframe time


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2019-06-22
    相关资源
    最近更新 更多