[R] retaining "POSIXct" formatting when using apply(muff, FUN=MAX) on POSIXct dataframe?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jan 9 01:01:25 CET 2008


jim holtman wrote:
> Now take a look at what happens when you convert your dataframe to a matrix:
>
>   
>> x <- as.matrix(mydf)
>> str(x)
>>     
> ....
> It is a character matrix.
>
>   
This is a bit confusing (but deliberate; as.matrix.data.frame explicitly 
counts POSIXct as non-numeric):

 > mode(as.matrix(mydf)[1,1])
[1] "character"
 > mode(as.matrix(mydf[1,1]))
[1] "numeric"
 > mode(as.matrix(mydf[1,]))
[1] "character"
 > mode(as.matrix(mydf[,1]))
[1] "numeric"
 > mode(unlist(mydf[1,]))
[1] "numeric"
 > unlist(mydf[1,])
        X1         X2         X3         X4         X5         
X6         X7
1199835988 1199835988 1199835988 1199835988 1199835988 1199835988 
1199835988
        X8         X9        X10
1199835988 1199835988 1199835988


And hence also

 > sapply(mydf,max)
        X1         X2         X3         X4         X5         
X6         X7
1199835988 1199835988 1199835988 1199835988 1199835988 1199835988 
1199835988
        X8         X9        X10
1199835988 1199835988 1199835988

The ticket seems to be

 > do.call(c, lapply(mydf,max))
                       X1                        
X2                        X3
"2008-01-09 00:46:27 CET" "2008-01-09 00:46:27 CET" "2008-01-09 00:46:27 
CET"
                       X4                        
X5                        X6
"2008-01-09 00:46:27 CET" "2008-01-09 00:46:27 CET" "2008-01-09 00:46:27 
CET"
                       X7                        
X8                        X9
"2008-01-09 00:46:27 CET" "2008-01-09 00:46:27 CET" "2008-01-09 00:46:27 
CET"
                      X10
"2008-01-09 00:46:27 CET"

 > class(do.call(c,lapply(mydf,max)))
[1] "POSIXt"  "POSIXct"



-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list