Reputation: 111
I have a dataframe of timestmaps in R for which the dates are completely gibberish. I am looking to extract time information and substitute the correct date. But I am running into some issue. I have shown a part of the dataframe (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")
If I look at the structure it is all in POSIXct format
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" ...
But when I am trying to extract the time, it is giving me an error
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”
I can extract time for individual columns but not for the whole dataframe. I do have 42 columns, so I was hoping for a cleaner method. This is what I am going for:
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)
Upvotes: 0
Views: 197
Reputation: 16277
You have to use apply
by column (2):
apply(Time_df,2,strftime, format="%H:%M:%S")
Time1 Time2 Time3 Time4 Time5
[1,] "07:00:00" "19:00:00" "00:00:00" "00:00:00" "00:00:00"
[2,] "00:00:00" "00:00:00" "00:00:00" "00:00:00" "00:00:00"
[3,] "00:00:00" "00:00:00" "00:00:00" "00:00:00" "00:00:00"
[4,] "06:00:00" "22:00:00" "00:00:00" "00:00:00" "00:00:00"
[5,] "08:00:00" "21:00:00" "00:00:00" "00:00:00" "00:00:00"
Upvotes: 1