Skip to content
Prev 387586 / 398502 Next

Converting POSIXct format date to Character format

Hi ma015k3113,
I suspect that you are asking the wrong question.

# create an example data frame with an extra field
PLC<-read.table(text="YEAR_END_Date     EPS   junk
2010-09-10        .10 A
2009-08-10        .20 B",
header=TRUE,
stringsAsFactors=FALSE)
# first, I think that you may already have the date in character format
# if you get this result, you don't need to convert it
sapply(PLC,"class")
YEAR_END_Date           EPS          junk
 "character"     "numeric"   "character"
# however, if you get this result
sapply(PLC,"class")
$YEAR_END_Date
[1] "POSIXlt" "POSIXt"

$EPS
[1] "numeric"

$junk
[1] "character"
# then you do have a POSIX date in the first field,
# so you can do this:
PLC$YEAR_END_Date<-format(PLC$YEAR_END_Date,format="%Y-%m-%d")
# then if you want to select those two fields for further processing
# use this expression
PLC[,c("YEAR_END_Date","EPS")]

Jim

On Thu, Mar 25, 2021 at 6:11 AM e-mail ma015k3113 via R-help
<r-help at r-project.org> wrote: