0

DISCLAIMER: I assume 'cache' and 'environment' are the right words here, though I am new to programming, so I may have misunderstood the terminology.

I have recently been trying to get to grips with the package fasttime for R, and I realise know there it has a behavior that I've not seem in documentation and also has been the root of my problems getting it to function.

TL;DR - If I use fast time to convert to POSIXct from a parent object into child one, then use it in a different way to calculate from the intact parent to child 2, child two will always have the result of child two, even if they should be different when calculated entirely independently.

Long version -

I have a long list of dates and times as characters to convert into POSIX (see this question)

And example data set and working file can be found here.

When I read the example file I need to build a single DateTime column, then convert it to POSIXct.

First I read:

setwd("~/GitHub/fasttimeError")

example <- fread("datesAndTimes.csv")
# read example file of dates and times

Then I paste:

example[ , DateTime := paste(ConsumptionDate, variable)]
# paste dates with times to make DateTime single column for POSIX conversion

Then I attempt to convert:

library(fasttime)
# load library for fastPOSIXct

test1 <- example[ , DateTime := fastPOSIXct(x = DateTime)]
# this will read the data as starting in 2006

test2 <- example[ , DateTime := fastPOSIXct(sub('(\\d*)/(\\d*)/(\\d*) (.*)', '\\3-\\1-\\2 \\4', DateTime))]
# this should produce an identical file to test 1

Darn, neither process worked correctly. They both produce a data set starting at 2006.

But for some reason, if I clear the whole working environment (in RStudio), and re run the exact same code but SKIP the line that creates test1, then my object labelled test2 converts perfectly.

Why?

Community
  • 1
  • 1
DaveRGP
  • 1,430
  • 15
  • 34
  • 2
    Do you you realise that `:=` modifies in place? Did you try your code not within `data.table` environment? – David Arenburg Apr 27 '15 at 09:45
  • 1
    i.e., do `test1 <- copy(example); test1[ , DateTime := fastPOSIXct(x = DateTime)]` and similar for `test2`. – Roland Apr 27 '15 at 09:54
  • I do, however I also thought that as I was assigning to a new object, the copy took place first, then the assignment? From what I've seen of data.table posts around the place a lot of the corrections people suggest are just for this reason, so that the file isn't copied over. Is this wrong? – DaveRGP Apr 27 '15 at 09:54
  • 2
    You should study data.table's documentation and vignettes. – Roland Apr 27 '15 at 09:56
  • I see. If you put it the form of answer I will close the question. thank you. – DaveRGP Apr 27 '15 at 09:58

0 Answers0