Skip to content
Prev 21154 / 63424 Next

.Call and data frames

Hin-Tak Leung wrote:
FWIW, a factor consists of all these things internally:
(1) TYPEOF(colData)  is INTSXP
(2) attr(colData, "levels") exists and is a STRSXP type
(string representation for the levels).
(3) class(colData) = "factor"

if coerVector() doesn't do it, you can test for (3) in your C code,
and use the integer vector in (1) to index into the string vector in (2)
to regenerate the string vector manually.

Not all of this is correct, just an idea:

class = getAttrib(colData, R_ClassSymbol);
...
if (.../* do some test on class */...)
{
     levels = getAttrib(colData, "levels");
     PROTECT(back_to_str = allocVector(STRSXP, LENGTH(colData));
     for(int i = 0; i < LENGTH=(colData) ; i++)
     {
       SET_STRING_ELT(back_to_str, i,
           mkChar(STRING_ELT(levels, INTEGER(colData)[i])));
     }
}