How to prevent write.csv from changing POSIXct, dates and times class back to character/factors?

后端 未结 3 344
既然无缘
既然无缘 2021-01-03 04:50

I have a .csv file with one field each for datetime, date and time. Originally they are all character fields and I have converted them accordingly. At the end of my code, if

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 05:36

    If you want to preserve all of the time information so it can be read in again, this recipe should work:

    dat <- data.frame(time=as.POSIXlt("2013-04-25 09:00 BST"), quantity=1)
    dat2 <- dat
    dat2$time <- format(dat2$time, usetz=TRUE)
    write.csv(dat2, "time.csv", row.names=FALSE)
    

    It gives the following CSV file:

    "time","quantity"
    "2013-04-25 09:00:00 BST",1
    

    in which the timezone information is presented explicitly; if you apply write.csv to the original dat, the formatting is lost.

提交回复
热议问题