问题
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?
来源:https://stackoverflow.com/questions/29892116/does-fasttime-cache-values-outside-of-its-environment-if-so-why