![](/img/trans.png)
[英]How do I get R to recognise a date-time column in my dataframe, DF? MY attempt at the solution is below
[英]How do I get R to recognise a “time” column within a dataframe?
我创建了一个数据框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" ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.