Despite using the stringsASFactors = FALSE argument, rbind() is converting my POSIXct vectors into class character. I've checked out discussions on cbind/rbind type functions and how they handle POSIXct (see here, here, and here) but unfortunately they don't help in this case.
Here I create POSIXct vectors as the basis for the rows within a dataframe. When I use rbind() withing as.data.frame() and use stringsASFactors = FALSE, the POSIXct values are changed to class character. I do know that it is rbind() and not as.data.frame() that is converting the class and there does not seem to be any way of preventing this.
While I'm familiar with various date time structures, I did some research and I have no clue what system it is using to store the date-time or how to convert it back (yes, I already tried as.POSIXct()).
So the question is, how do I convert these back to POSIXct? (If there is some magical way to have rbind() not convert POSIXct to character, I'll take that too.) Thanks!
####Build example####
d1 <- as.POSIXct("2020-06-30 8:00")
d2 <- as.POSIXct("2020-06-30 9:00")
d3 <- as.POSIXct("2020-06-30 10:00")
d4 <- as.POSIXct("2020-06-30 16:00")
s1 <- c(d1, d2, d3, d4, "", "")
d1 <- as.POSIXct("2020-06-30 8:00")
d2 <- as.POSIXct("2020-06-30 10:00")
d3 <- as.POSIXct("2020-06-30 11:00")
d4 <- as.POSIXct("2020-06-30 12:00")
d5 <- as.POSIXct("2020-06-30 12:30")
d6 <- as.POSIXct("2020-06-30 16:30")
s2 <- c(d1, d2, d3, d4, d5, d6)
d1 <- as.POSIXct("2020-06-30 8:00")
d2 <- as.POSIXct("2020-06-30 13:00")
d3 <- as.POSIXct("2020-06-30 13:45")
d4 <- as.POSIXct("2020-06-30 16:30")
s3 <- c(d1, d2, d3, d4, "", "")
d3 <- as.POSIXct("2020-06-30 13:25")
s4 <- c(d1, d2, d3, d4, "", "")
d2 <- as.POSIXct("2020-06-30 16:00")
s5 <- c(d1, d2, "", "", "", "")
####Build data frame####
timedata <- as.data.frame(rbind(s1, s2, s3, s4, s5), stringsAsFactors = FALSE)
names(timedata) <- c("T1", "T2", "T3", "T4", "T5", "T6")
for(c in seq(from = 1, to = 6, by = 1)) {
timedata[,c] <- as.POSIXct(timedata[,c], tzone = "PDT")
}
P.S. There is some documentation for as.POSIXct() which discusses how to convert gibberish with a specific origin back into a functional POSIXct. But the examples are using the varying structures provided by SAS, SPSS, STATA, and Matlab, not internal R.
If you wrap s1
, ..., s5
in as.character()
prior to rbind()
it seems to behave as you'd like.
timedata <- as.data.frame(rbind(as.character(s1),
as.character(s2),
as.character(s3),
as.character(s4),
as.character(s5)), stringsAsFactors = FALSE)
Then you can do what you did and convert back.
Another option is to keep the rbind()
as you are doing, and then change your loop as follows. This is all due to the supported classes of each function.
The key is hidden away in the Note section of help(as.POSIXct)
.
the origin of time for the "POSIXct" class, ‘1970-01-01 00:00.00 UTC’
for(c in seq(from = 1, to = 6, by = 1)) {
timedata[,c] <- as.POSIXct(as.numeric(timedata[,c]), origin = "1970-01-01 00:00:00", tzone = "PDT")
}