Message-ID: <18825.3921.722371.593450@ron.nulle.part>
Date: Tue, 3 Feb 2009 21:45:21 -0600
To: personal-kdbplus@googlegroups.com
Subject: Improved kdb+/R conversion for datetime
X-Mailer: VM 8.0.9 under Emacs 23.0.60.1 (i486-pc-linux-gnu)
From: Dirk Eddelbuettel
X-Google-Approved: charlie@kx.com via web at 2009-02-04 06:40:50
I have started to experiment with kdb+ and q. As my preferred tool for analysis
is R, I started to work with the files in
https://code.kx.com/trac/browser/kx/kdb%2B/interfaces/r
which I turned into a (local) CRAN-style R package. I noticed one clear
shortcoming of this otherwise rather nice kdb+/R interface: datetime objects
got dropped to int, were only handled one-at-a-time rather than vectorised
and lead to segfaults due to what looks like a leftover reference count
decrementor r0(x).
The version below works on vectors as well as scalars, and converts to floats
with fractional seconds since the Unix epoch along with POSIXt / POSIXct
class attributes just like other R Datetime objects. [The same can be done
for dates, only convert to INTSXP and set only one class attribute, “Date”.]
Hope this helps, Dirk
static SEXP from_datetime_kobject(K x)
{
SEXP result;
int i, length = x->n;
if (scalar(x)) {
result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = (kF(x)[0] + 10957) * 86400;
} else {
result = PROTECT(allocVector(REALSXP, length));
for(i = 0; i < length; i++) {
REAL(result)[i] = (kF(x)[i] + 10957) * 86400;
}
}
SEXP datetimeclass = PROTECT(allocVector(STRSXP,2));
SET_STRING_ELT(datetimeclass, 0, mkChar(“POSIXt”));
SET_STRING_ELT(datetimeclass, 1, mkChar(“POSIXct”));
setAttrib(result, R_ClassSymbol, datetimeclass);
UNPROTECT(2);
return result;
}
–
Three out of two people have difficulties with fractions.
Thanks. I updated the date and datetime functions as suggested.On Feb 4, 11:45?am, Dirk Eddelbuettel <e…> wrote:> I have started to experiment with kdb+ and q. As my preferred tool for analysis> is R, I started to work with the files in>> ? ?https://code.kx.com/trac/browser/kx/kdb%2B/interfaces/r>> which I turned into a (local) CRAN-style R package. ?I noticed one clear> shortcoming of this otherwise rather nice kdb+/R interface: datetime objects> got dropped to int, were only handled one-at-a-time rather than vectorised> and lead to segfaults due to what looks like a leftover reference count> decrementor r0(x).>> The version below works on vectors as well as scalars, and converts to floats> with fractional seconds since the Unix epoch along with POSIXt / POSIXct> class attributes just like other R Datetime objects. ?[The same can be done> for dates, only convert to INTSXP and set only one class attribute, “Date”.]>> Hope this helps, ?Dirk>> static SEXP from_datetime_kobject(K x)> {> ? ? ? ? SEXP result;> ? ? ? ? int i, length = x->n;> ? ? ? ? if (scalar(x)) {> ? ? ? ? ? ? ? ? result = PROTECT(allocVector(REALSXP, 1));> ? ? ? ? ? ? ? ? REAL(result)[0] = (kF(x)[0] + 10957) * 86400;> ? ? ? ? } else {> ? ? ? ? ? ? ? ? result = PROTECT(allocVector(REALSXP, length));> ? ? ? ? ? ? ? ? for(i = 0; i < length; i++) {> ? ? ? ? ? ? ? ? ? ? ? ? REAL(result)[i] = (kF(x)[i] + 10957) * 86400;> ? ? ? ? ? ? ? ? }> ? ? ? ? }> ? ? ? ? SEXP datetimeclass = PROTECT(allocVector(STRSXP,2));> ? ? ? ? SET_STRING_ELT(datetimeclass, 0, mkChar(“POSIXt”));> ? ? ? ? SET_STRING_ELT(datetimeclass, 1, mkChar(“POSIXct”));> ? ? ? ? setAttrib(result, R_ClassSymbol, datetimeclass);> ? ? ? ? UNPROTECT(2);> ? ? ? ? return result;>> }>> –> Three out of two people have difficulties with fractions.</e…>