从R

时间:2017-03-27 18:28:59

标签: r posixct

我在R中有一个时间图的数据框,日期完全是胡言乱语。我希望提取时间信息并替换正确的日期。但我遇到了一些问题。我已经展示了数据帧的一部分(Time_df)

Time_df = 
                  Time1               Time2      Time3      Time4      Time5
102 1970-01-01 07:00:00 1970-01-01 19:00:00 1970-01-01 1970-01-01 1970-01-01
103 1970-01-01 00:00:00 1970-01-02 00:00:00 1970-01-01 1970-01-01 1970-01-01
104 1970-01-01 00:00:00 1970-01-02 00:00:00 1970-01-01 1970-01-01 1970-01-01
105 1970-01-01 06:00:00 1970-01-01 22:00:00 1970-01-01 1970-01-01 1970-01-01
106 1970-01-01 08:00:00 1970-01-01 21:00:00 1970-01-01 1970-01-01 1970-01-01

dput(Time_df) =

 structure(list(Time1 = structure(c(43200, 18000, 18000, 39600, 
46800), class = c("POSIXct", "POSIXt"), tzone = ""), Time2 = structure(c(86400, 
104400, 104400, 97200, 93600), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Time3 = structure(c(18000, 18000, 18000, 18000, 18000), class = c("POSIXct", 
    "POSIXt"), tzone = ""), Time4 = structure(c(18000, 18000, 
    18000, 18000, 18000), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Time5 = structure(c(18000, 18000, 18000, 18000, 18000), class = c("POSIXct", 
    "POSIXt"), tzone = "")), .Names = c("Time1", "Time2", "Time3", 
"Time4", "Time5"), row.names = 102:106, class = "data.frame")

如果我查看结构,它全部采用POSIXct格式

str(Time_df)
'data.frame':   5 obs. of  5 variables:
 $ Time1: POSIXct, format: "1970-01-01 07:00:00" "1970-01-01 00:00:00" "1970-01-01 00:00:00" ...
 $ Time2: POSIXct, format: "1970-01-01 19:00:00" "1970-01-02 00:00:00" "1970-01-02 00:00:00" ...
 $ Time3: POSIXct, format: "1970-01-01" "1970-01-01" "1970-01-01" ...
 $ Time4: POSIXct, format: "1970-01-01" "1970-01-01" "1970-01-01" ...
 $ Time5: POSIXct, format: "1970-01-01" "1970-01-01" "1970-01-01" ...

但是当我试图提取时间时,却给了我一个错误

times = strftime(Time_df, format="%H:%M:%S")
Error in as.POSIXlt.default(x, tz = tz) : 
  do not know how to convert 'x' to class “POSIXlt”

我可以为单个列提取时间,但不能为整个数据帧提取时间。我确实有42列,所以我希望有一个更清洁的方法。这就是我想要的:

Mon = rep("03/20/17", 6)
Tue = rep("03/21/17", 6)
Wed = rep("03/22/17", 6)
Thu = rep("03/23/17", 6)
Fri = rep("03/24/17", 6)
Sat = rep("03/25/17", 6)
Sun = rep("03/26/17", 6)

dates = c(Mon, Tue, Wed, Thu, Fri, Sat, Sun)
t = strftime(Time_df, format="%H:%M:%S")
x = paste(dates, t) 

1 个答案:

答案 0 :(得分:1)

您必须按列(2)使用$test[$url][$word] = $freq;

apply