-2
k <- file.Folder$LPRS.time[1] + lag*60
> k
[1] "2017-03-31 00:15:00 IST"
> file.Folder$start.time[1] <- file.Folder$LPRS.time[1] + lag*60
> file.Folder$start.time[1]
[1] 1490899500

file.Folder$start.time is a new column created just at the last step. Why is it showing properly as date in scenario 1 and as number in scenario 2??

Edgar Santos
  • 3,426
  • 2
  • 17
  • 29
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56

2 Answers2

0

Option 1: You could use lubridate package since the arithmetic is easy.

library("lubridate")
x <- ymd_hms("2017-03-31 00:15:00 IST", tz = "Asia/Istanbul")
lag = hours(4)
x + lag * 60

Results:

[1] "2017-04-10 00:15:00 EEST"

Option 2: Convert the numeric result as.POSIXct: as.POSIXct(1490899500, tz = "Asia/Istanbul", origin = "1970-01-01 00:00.00 UTC")

Edgar Santos
  • 3,426
  • 2
  • 17
  • 29
0

To answer the question "why?" we would need to see the results of str(file.Folder), in particular the class of file.Folder$start.time: This short test session shows that assigning to a non-time, numeric column will coerce the result to the underlying seconds since the start of the epoch. This effect is not the result if assigning to an individual values using "[":

> test <- data.frame(tm=Sys.time() , num=1)
> test$num[1] <- test$tm +60
> test
                   tm        num
1 2017-03-30 16:49:03 1490917803
> test <- cbind(test, other.tm=Sys.time())
> test$othernum <- test$tm +60
> test
                   tm        num            other.tm            othernum
1 2017-03-30 16:49:03 1490917803 2017-03-30 16:50:25 2017-03-30 16:50:03
> test$othernum[1] <- test$tm +60
> test
                   tm        num            other.tm            othernum
1 2017-03-30 16:49:03 1490917803 2017-03-30 16:50:25 2017-03-30 16:50:03
IRTFM
  • 258,963
  • 21
  • 364
  • 487