From rory.winston at gmail.com  Thu Dec 27 09:14:07 2012
From: rory.winston at gmail.com (Rory Winston)
Date: Thu, 27 Dec 2012 08:14:07 +0000
Subject: [Rd] Creating a Factor Object in C code?
Message-ID: <DDF515A2-D7C3-4123-B8EC-DD6D864AC665@gmail.com>

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


[Rd] Creating a Factor Object in C code?
Rory Winston