[Rd] Data table in C

Seth Falcon sfalcon at fhcrc.org
Fri Nov 17 18:22:16 CET 2006

"Tom McCallum" <tom.mccallum at levelelimited.com> writes:

> After getting one list done, I am now struggling to form a data frame in C.
> I tried to do a list of lists which gives me :
> $<NA>
> $<NA>[[1]]
> [1] "BID"
> $<NA>[[2]]
> [1] 0.6718
> and then as.data.frame them in R but this gives me

One approach is to return a named list and then turn that into a
data.frame like:

ret <- .Call("yourFunc", ...)
attr(ret, "row.names") <- as.integer(indx)
class(ret) <- "data.frame"

In C, you need to create a list (VECSXP) and a character vector
(STRSXP) for the names and then put the names on the list.

PROTECT(ret = allocVector(VECSXP, N));
PROTECT(retnames = allocVector(STRSXP, N));

/* fill list and names */

SET_NAMES(ret, retnames);

+ seth

More information about the R-devel mailing list