Thomas Pujol
2008-Jan-08 17:28 UTC
[R] retaining "POSIXct" formatting when using apply(muff, FUN=MAX) on POSIXct dataframe?
How do I retain "POSIXct" formatting when using apply, with FUN=max? #example: mydata <- rep(Sys.time(), 10) mydf <- data.frame(matrix(data=mydata, nrow=2, ncol=length(mydata) ) ) for(i in seq(mydf))class(mydf[[i]]) <- class(mydata) str(mydf) maxdates <- apply(mydf,2,max,na.rm=T) str(maxdates) #Why is the formattign now "chr", and not "POSIXct"? #Is there a good way to retin "POSIXct" formatting? --------------------------------- [[alternative HTML version deleted]]
jim holtman
2008-Jan-08 23:35 UTC
[R] retaining "POSIXct" formatting when using apply(muff, FUN=MAX) on POSIXct dataframe?
It all depends on what you are doing. Take a look at 'apply'; it says: If X is not an array but has a dimension attribute, apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., data frames) or via as.array. Now take a look at what happens when you convert your dataframe to a matrix:> x <- as.matrix(mydf) > str(x)chr [1:2, 1:10] "2008-01-08 23:29:25" "2008-01-08 23:29:25" "2008-01-08 23:29:25" ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:10] "X1" "X2" "X3" "X4" ...>It is a character matrix. On Jan 8, 2008 12:28 PM, Thomas Pujol <thomas.pujol at yahoo.com> wrote:> How do I retain "POSIXct" formatting when using apply, with FUN=max? > > #example: > mydata <- rep(Sys.time(), 10) > mydf <- data.frame(matrix(data=mydata, nrow=2, ncol=length(mydata) ) ) > for(i in seq(mydf))class(mydf[[i]]) <- class(mydata) > str(mydf) > maxdates <- apply(mydf,2,max,na.rm=T) > str(maxdates) > > #Why is the formattign now "chr", and not "POSIXct"? > #Is there a good way to retin "POSIXct" formatting? > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Apparently Analagous Threads
- retaining formatting when converting a vector to a matrix/data.frame?
- calculating p-values of columns in a dataframe
- aggregate.data.frame - prevent conversion to factors? show statistics for NA values of "by" variable?
- add only the 1st of May with POSIXct
- add only the 1st of May with POSIXct