From sfalcon at fhcrc.org  Fri Nov 17 18:22:16 2006
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Fri, 17 Nov 2006 09:22:16 -0800
Subject: [Rd] Data table in C
In-Reply-To: <op.ti53ujp0ffxks9@norisk> (Tom McCallum's message of "Fri,
	17 Nov 2006 15:55:21 -0000")
References: <op.ti53ujp0ffxks9@norisk>
Message-ID: <m2psbmf0on.fsf@ziti.local>

"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
[snip]
>
> 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



[Rd] Data table in C
Seth Falcon