Thomas Pujol
2008-Jan-03 17:01 UTC
[R] retaining formatting when converting a vector to a matrix/data.frame?
Please see example code below.
I have a vector ("mydata") of length 10. "mydata" can
have various formats (e.g. numeric, text, POSIXct, etc) I use the matrix and
data.frame functions to convert "mydata" to a dataframe
("mydf") of 2 columns and 5 rows.
What is a "good" way to ensure that the format is retained when I
create the data.frame?
Currently, data in the "POSIXct" format is converted to numeric.
Also, for my edification, is there a "good" way to convert numeric
values such as these back to the original "POSIXct" format/values?
Much thanks!
#here I lose the "POSIXct" formatting
mydata <- rep(Sys.time(), 10)
str(mydata)
mydf <- data.frame(matrix(data=mydata, nrow=5, ncol=2))
str(mydf)
#here using only "matrix" I also lose the "POSIXct"
formatting
mydata <- rep(Sys.time(), 10)
str(mydata)
mydf <- matrix(data=mydata, nrow=5, ncol=2)
str(mydf)
#this works as intended
mydata <- rep(1:10)
str(mydata)
mydf <- data.frame(matrix(data=mydata, nrow=5, ncol=2))
str(mydf)
---------------------------------
[[alternative HTML version deleted]]
Bert Gunter
2008-Jan-03 18:07 UTC
[R] retaining formatting when converting a vector to amatrix/data.frame?
matrix() does not preserve class attributes, as it merely creates a matrix
of mode the mode of its argument (possibly after some coercion), which is
"numeric" for POSIXct, as you know.
So why not just manually set the class attribute after creation of the
data.frame:
mydf <- data.frame(matrix(mydata,ncol=2))
for(i in seq(mydf))class(mydf[[i]]) <- class(mydata)
Cheers,
Bert Gunter
Genentech
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Thomas Pujol
Sent: Thursday, January 03, 2008 9:02 AM
To: r-help at stat.math.ethz.ch
Subject: [R] retaining formatting when converting a vector to
amatrix/data.frame?
Please see example code below.
I have a vector ("mydata") of length 10. "mydata" can
have various
formats (e.g. numeric, text, POSIXct, etc) I use the matrix and data.frame
functions to convert "mydata" to a dataframe ("mydf") of 2
columns and 5
rows.
What is a "good" way to ensure that the format is retained when I
create
the data.frame?
Currently, data in the "POSIXct" format is converted to numeric.
Also, for my edification, is there a "good" way to convert numeric
values
such as these back to the original "POSIXct" format/values?
Much thanks!
#here I lose the "POSIXct" formatting
mydata <- rep(Sys.time(), 10)
str(mydata)
mydf <- data.frame(matrix(data=mydata, nrow=5, ncol=2))
str(mydf)
#here using only "matrix" I also lose the "POSIXct"
formatting
mydata <- rep(Sys.time(), 10)
str(mydata)
mydf <- matrix(data=mydata, nrow=5, ncol=2)
str(mydf)
#this works as intended
mydata <- rep(1:10)
str(mydata)
mydf <- data.frame(matrix(data=mydata, nrow=5, ncol=2))
str(mydf)
---------------------------------
[[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
2008-Jan-04 02:54 UTC
[R] retaining formatting when converting a vector to a matrix/data.frame?
Here is a function that I use since the numeric value is the number of
seconds from 1/1/1970, which is the Unix time base that I am also
using:
unix2POSIXct <- function (time) structure(time, class = c("POSIXt",
"POSIXct"))
It just reassigns the proper class.
On Jan 3, 2008 12:01 PM, Thomas Pujol <thomas.pujol at yahoo.com>
wrote:> Please see example code below.
>
> I have a vector ("mydata") of length 10. "mydata" can
have various formats (e.g. numeric, text, POSIXct, etc) I use the matrix and
data.frame functions to convert "mydata" to a dataframe
("mydf") of 2 columns and 5 rows.
>
> What is a "good" way to ensure that the format is retained when
I create the data.frame?
> Currently, data in the "POSIXct" format is converted to numeric.
>
> Also, for my edification, is there a "good" way to convert
numeric values such as these back to the original "POSIXct"
format/values?
>
> Much thanks!
>
> #here I lose the "POSIXct" formatting
>
> mydata <- rep(Sys.time(), 10)
> str(mydata)
> mydf <- data.frame(matrix(data=mydata, nrow=5, ncol=2))
> str(mydf)
>
> #here using only "matrix" I also lose the "POSIXct"
formatting
>
> mydata <- rep(Sys.time(), 10)
> str(mydata)
> mydf <- matrix(data=mydata, nrow=5, ncol=2)
> str(mydf)
>
>
> #this works as intended
> mydata <- rep(1:10)
> str(mydata)
> mydf <- data.frame(matrix(data=mydata, nrow=5, ncol=2))
> str(mydf)
>
>
>
> ---------------------------------
>
> [[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?