1

I want to change the day part of a year-month-day structure. For example, from 1990-01-03 to 1990-01-01. Is the code below correct?

d<-as.POSIXlt("1990-01-03")
d$mday<-1

wday and yday are not changed, so the result is somewhat inconsistent. I use the result only for conversion to character strings. Other date representations I know(Date, POSIXct) are inconvenient for this task.

Sotos
  • 51,121
  • 6
  • 32
  • 66
elflyao
  • 367
  • 3
  • 9

1 Answers1

1

use lubridate package to parse date and then subtract days

library(lubridate)

a=ymd("1990-01-03")

From How to subtract days in R?

a-2

or day(a)=1

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
  • There is no need in `lubridate` here. They can just do everything with `as.Date` here. And if this is a dupe- it should be closed rather adding answers to it. – David Arenburg Jul 06 '17 at 11:25