Hey all,

Virgin post to this list - hope I've got it right ;o)

I've been learning R intensively the last two weeks and gone from newbie
status to *reasonably* comfortable with it.

Here's an issue I just cannot solve however as it appears to be some kind
of bug in R itself.  But I won't claim that for sure.

I have a function as follows:


FindHighRow <- function(searchVector, highColumnIndex, thisDate, nextDate)
{
  dateFilter <- GetDateRangeString(thisDate,nextDate)
  filtered <- searchVector[dateFilter]
  returnRow <- filtered[which.max(filtered[,highColumnIndex])]
  returnRow
}


Running the lines BY HAND works fine.  But as soon as I call it as a
function (passing in *exactly* the same values - and I have definitely
checked and double-checked), the second line (filtered <-
searchVector[dateFilter]) throws this error .

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,  :

  missing value where TRUE/FALSE needed

In addition: Warning messages:

1: In as_numeric(YYYY) : NAs introduced by coercion

2: In as_numeric(YYYY) : NAs introduced by coercion


Checking with debugger() reveals this

> debugger()

Message:  Error in if (length(c(year, month, day, hour, min, sec)) == 6 &&
c(year,  :

  missing value where TRUE/FALSE needed

Available environments had calls:

1: FindHighRow(dateTable, theTimeSeries, 2, 3)

2: #4: searchVector[dateFilter]

3: #4: `[.xts`(searchVector, dateFilter)

4: .parseISO8601(ii, .index(x)[1], .index(x)[nr], tz = tz)

5: as.POSIXlt(do.call(lastof, parse.side(intervals[2], intervals[1])))

6: do.call(lastof, parse.side(intervals[2], intervals[1]))

7: function (year = 1970, month = 12, day = 31, hour = 23, min = 59, sec =
59, subsec = 0.99999, tz = "")

{

    if (!missing(sec) && sec%%1 != 0)

        subsec <- 0

I then came across this post

http://r-forge.r-project.org/tracker/index.php?func=detail&aid=2116&group_id=118&atid=516

While running demo/macd.R in the quantstrat package with --vanilla, I got
the following error:

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year, :
missing value where TRUE/FALSE needed

It appears that this error is generated by .parse8061(), because there is
no TZ defined. Although there is a test for TZ=='' in the .parse8061()
function, that does not seem to work .... (didn't look any deeper, sorry
;-) )

Setting TZ to eg. "UTC" fixes the problem.

I've tried setting TZ (timezone) to both "UTC" and "GMT"

> Sys.getenv("TZ")

[1] ""

> Sys.setenv(TZ='UTC')

> Sys.getenv("TZ")

[1] "UTC"

> Sys.getenv("TZ") %in% c("", "GMT", "UTC")

[1] TRUE

But running my function again I still get the same error

> FindHighRow(filteredDates,theTimeSeries,2,3)

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,  :

  missing value where TRUE/FALSE needed

In addition: Warning messages:

1: In as_numeric(YYYY) : NAs introduced by coercion

2: In as_numeric(YYYY) : NAs introduced by coercion

> FindHighRow

function(searchVector, highColumnIndex, thisDate, nextDate)

{

  dateFilter <- GetDateRangeString(thisDate,nextDate)

  filtered <- searchVector[dateFilter]

  returnRow <- filtered[which.max(filtered[,highColumnIndex])]

  returnRow

}

> dateFilter <- GetDateRangeString(thisDate,nextDate)

> dateFilter

[1] "2007-10-01::2008-01-01"

> filtered <- searchVector[dateFilter]

> returnRow <- filtered[which.max(filtered[,highColumnIndex])]

> head(returnRow,10)

             Open      High     Low       Close

2007-10-10   1564.72   1576.09  1546.72   1554.41


So as I hope you can see, it works by hand but not within a function.

That's why I think it's a bug.


Just for replicability (I hope)

GetDateRangeString <- function(thisDate,nextDate)
{
  GetDateRangeString <-
paste(EnsureDate(thisDate),"::",EnsureDate(nextDate),sep="")
}

EnsureDate <- function(maybeWrongFormat)
{
  as.POSIXct(strptime(maybeWrongFormat, "%Y-%m-%d"))
}



And theTimeSeries is

theTimeSeries <- getSymbols(tckr, from="2000-01-01",
to="2013-01-01",auto.assign=FALSE)


I'm too much of a newbie to suggest how to fix this (or what else it might
be) so I thought I'd report it to the list and see what happens.

If any of this code doesn't work (I've cleaved it out of a bigger project)
just let me know and I'll fill in any missing pieces.

Regards,

Bit Rocker

	[[alternative HTML version deleted]]

