传递一个数据帧从到R和C使用.CALL() [英] Passing a data frame from-to R and C using .call()

查看:158
本文介绍了传递一个数据帧从到R和C使用.CALL()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有通过用自R任意列(整数/因子,数字,字符数据)至c和背面的数据帧的一个一般方法是什么?指针关闭足够的例子将是极大的AP preciated。

Is there a general way of passing a data frame with arbitrary columns (integer/factor, numeric, character data) from r to c and back? Pointers to close enough examples would be greatly appreciated.

感谢。

RT

推荐答案

一个data.frame是一个列表,所以沿行

A data.frame is a list, so along the lines of

#include <Rdefines.h>

SEXP df_fun(SEXP df)
{
    int i, len = Rf_length(df);
    SEXP result;
    PROTECT(result = NEW_CHARACTER(len));
    for (i = 0; i < len; ++i)
        switch(TYPEOF(VECTOR_ELT(df, i))) {
        case INTSXP:
            SET_STRING_ELT(result, i, mkChar("integer"));
            break;
        case REALSXP:
            SET_STRING_ELT(result, i, mkChar("numeric"));
            break;
        default:
            SET_STRING_ELT(result, i, mkChar("other"));
            break;
        };
        UNPROTECT(1);
    return result;
}

再经过研究CMD SHLIB df_fun.c

> dyn.load("df_fun.so")
> df=data.frame(x=1:5, y=letters[1:5], z=pi, stringsAsFactors=FALSE)
> .Call("df_fun", df)
[1] "integer" "other"   "numeric"

使用 GET_CLASS GET_ATTR 和Rdefines.h(或等同的功能,其他宏像 getAttrib )来发现对数据帧的其他信息。请注意,虽然一个data.frame具有可以从它的结构不同的API。所以,比如R函数 row.names 可以从存储在row.names属性的值返回不同的东西。我想大多数 .CALL 函数对原子向量操作,保持更复杂对象的操作在R平面。

Use GET_CLASS, GET_ATTR and other macros in Rdefines.h (or their equivalent functions, like getAttrib) to discover other information about the data frame. Note though that a data.frame has an API that can differ from its structure. So for instance the R function row.names can return something different from the value stored in the row.names attribute. I think most .Call functions operate on atomic vectors, keeping the manipulation of more complicated objects at the R level.

这篇关于传递一个数据帧从到R和C使用.CALL()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆