[Rd] Creating a Factor Object in C code?

Rory Winston rory.winston at gmail.com
Thu Dec 27 09:14:07 CET 2012


Hi guys

I am currently working on a small bit of bridging code between a database system and R. The database system has the concept of varchars, a la factors in R, as distinct from plain character strings. What I would like to do is when I receive a list of character strings from the remote database system that are of type varchar, turn these into a factor variable. This would ideally need to be done in C code, where the rest of the datatype translation is occuring. 

My first attempt was a bit naive (setting the factor class attribute on a vector of character strings, which obviously results in an error), looking at the R factor() implementation, I can see the core logic for factor conversion is:

 y <- unique(x)
 ind <- sort.list(y)
 y <- as.character(y)
 levels <- unique(y[ind])

So I am guessing this would need to be replicated in C? My question is - is it possible to create a fully-formed factor variable in C code (Ive struggled to find many / any examples), or should this be done in R when the call returns? I would like to make it seamless to the end user, so an automatic conversion to factors would be preferable..

Cheers
-- Rory


More information about the R-devel mailing list