Q:

Maintaining structure when converting to POSIXct

I'm attempting to convert two columns of unix times to actual date times. See example below:
df = as.matrix(data.frame(col1 = as.numeric(sample(1316006155:1316009155,10)), col2 = as.numeric(sample(1316006155:1316009155,10))))

as.POSIXct(df,origin = "1970-01-01",tz = "GMT")

Although the above works, I loose the structure of the data (i.e. creates one large vector). How do I maintain the structure (i.e. two columns that are each converted to POSIXct). Also, I thought if I had the unix times in a dataframe I could use the following apply code to change row by row (though I know that the vectorized from above is quicker), however this doesn't work. Why?
apply(df,2,function(x) as.POSIXct(x,origin = "1970-01-01",tz = "GMT"))

Thanks

A:

You're working with matrixes, but, "date" classes as "POSIXct" are not possible in a matrix. Particularly apply yields matrices as result, and thus coerces the "fresh" result into numeric again. The reason is that matrix() uses as.vector which actually has a mode= argument, but not for "date", "POSIXct", .... 
So you could either coerce to "character" format:
res1 <- apply(df, 2, function(x) as.character(as.POSIXct(x,origin="1970-01-01", tz="GMT")))

Or, more efficiently:
res1 <- array(as.character(as.POSIXct(df, origin="1970-01-01", tz="GMT")), 
              dim=dim(df), dimnames=dimnames(df))
#                        col1                  col2                 
#  [1,] "2011-09-14 13:59:23" "2011-09-14 13:21:50"
#  [2,] "2011-09-14 13:55:23" "2011-09-14 13:42:59"
#  [3,] "2011-09-14 13:35:31" "2011-09-14 13:18:39"
#  [4,] "2011-09-14 13:34:12" "2011-09-14 14:00:24"
#  [5,] "2011-09-14 13:36:46" "2011-09-14 13:56:52"
#  [6,] "2011-09-14 13:26:28" "2011-09-14 13:47:37"
#  [7,] "2011-09-14 13:50:51" "2011-09-14 13:30:53"
#  [8,] "2011-09-14 13:35:06" "2011-09-14 13:25:55"
#  [9,] "2011-09-14 13:38:01" "2011-09-14 13:37:41"
# [10,] "2011-09-14 13:50:26" "2011-09-14 13:31:26"

str(res1)
# chr [1:10, 1:2] "2011-09-14 13:59:23" "2011-09-14 13:55:23" ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "col1" "col2"

Or, coerce the matrix to "data.frame" which allows you to use lapply. A data.frame alows "POSIXct" classes.
res2 <- as.data.frame(lapply(as.data.frame(df), function(x) 
          as.POSIXct(x, origin="1970-01-01", tz="GMT")))
#                   col1                col2
# 1  2011-09-14 13:59:23 2011-09-14 13:21:50
# 2  2011-09-14 13:55:23 2011-09-14 13:42:59
# 3  2011-09-14 13:35:31 2011-09-14 13:18:39
# 4  2011-09-14 13:34:12 2011-09-14 14:00:24
# 5  2011-09-14 13:36:46 2011-09-14 13:56:52
# 6  2011-09-14 13:26:28 2011-09-14 13:47:37
# 7  2011-09-14 13:50:51 2011-09-14 13:30:53
# 8  2011-09-14 13:35:06 2011-09-14 13:25:55
# 9  2011-09-14 13:38:01 2011-09-14 13:37:41
# 10 2011-09-14 13:50:26 2011-09-14 13:31:26

str(res2)
# 'data.frame': 10 obs. of  2 variables:
# $ col1: POSIXct, format: "2011-09-14 13:59:23" "2011-09-14 13:55:23" ...
# $ col2: POSIXct, format: "2011-09-14 13:21:50" "2011-09-14 13:42:59" ...

Data:
df <- structure(c(1316008763, 1316008523, 1316007331, 1316007252, 1316007406, 
1316006788, 1316008251, 1316007306, 1316007481, 1316008226, 1316006510, 
1316007779, 1316006319, 1316008824, 1316008612, 1316008057, 1316007053, 
1316006755, 1316007461, 1316007086), .Dim = c(10L, 2L), .Dimnames = list(
    NULL, c("col1", "col2")))

