我有一个日期,时间的数据框。我用as.POSIXct读到它,但我不需要日期,我只需要时间。然后我想用我所拥有的时间制作盒子情节。我怎么只能读取时间数据?
C1
13/07/2017 15:15
13/07/2017 15:02
13/07/2017 14:40
13/07/2017 15:15
13/07/2017 15:10
13/07/2017 15:23
13/07/2017 14:56
13/07/2017 15:15
13/07/2017 15:15
13/07/2017 14:56
13/07/2017 14:56
13/07/2017 14:56
13/07/2017 15:15
13/07/2017 15:02
13/07/2017 15:10
df <- read.csv2(file="DF.csv")
df <- as.POSIXct(df$C1, format="%H:%M:%S", tz="GMT")
答案 0 :(得分:3)
$str = 'ans1,ans2,ans3';
答案 1 :(得分:3)
我尝试复制您的数据框并应用函数as.POSIXct()
以更好地复制您的情况。
您可以使用strftime()
函数拆分列,然后使用函数times()
创建按时间顺序排列的对象。
df = data.frame(C1 = c("13/07/2017 15:15","13/07/2017 15:02","13/07/2017 14:40","13/07/2017 15:15",
"13/07/2017 15:10","13/07/2017 15:23","13/07/2017 14:56","13/07/2017 15:15",
"13/07/2017 15:15","13/07/2017 14:56","13/07/2017 14:56","13/07/2017 14:56",
"13/07/2017 15:15","13/07/2017 15:02","13/07/2017 15:10")) #replicate data frame
df <- as.POSIXct(df$C1, format = "%d/%m/%Y %H:%M") #apply function to create a POSIXct object
然后,您需要应用于数据框的功能如下:
library(chron)
time <- times(strftime(df, format="%H:%M:%S")) #select only time and create a times object
这是结果:
time
[1] 15:15:00 15:02:00 14:40:00 15:15:00 15:10:00 15:23:00 14:56:00 15:15:00 15:15:00 14:56:00 14:56:00 14:56:00 15:15:00 15:02:00 15:10:00
class(time)
[1] "times"