2017-04-12 8 views
1

data.frame에서 날짜 정보가 들어있는 벡터를 추출하고 하위 세트로 만들려고합니다. DataFrame에서 DateVector을 성공적으로 추출 할 수 있습니다. 그러나 데이터를 부분 집합하려고 할 때 오류가 발생합니다.DateVector의 서브 세트

서브 세트 주위에 /* */이 주어진다면 아래 내용이 정상적으로 작동합니다.

error: invalid user-defined conversion from 'Rcpp::LogicalVector {aka Rcpp::Vector<10, Rcpp::PreserveStorage>}' to 'int' [-fpermissive]

Rcpp::DateVector StDate_sub = StDate[ind]

두 번째는 다음과 같습니다 : 나는이 문서에서 보았지만 방법을 찾을 수 없습니다

no known conversion from 'SEXP' to 'int' file585c1863151c.cpp:23:53: error: conversion from 'Rcpp::Date' to non-scalar type 'Rcpp::DateVector {aka Rcpp::oldDateVector}' requested

Rcpp::DateVector EtDate_sub = EtDate[ind];

Rcpp::cppFunction(' 
Rcpp::DataFrame test(DataFrame x, StringVector y) { 

    StringVector New = x["string_1"]; 
    std::string KEY = Rcpp::as<std::string>(y[0]); 
    Rcpp::LogicalVector ind(New.size()); 

    for(int i = 0; i < New.size(); i++){ 
    ind[i] = (New[i] == KEY); 
    } 


    Rcpp::StringVector st1 = x["string_1"]; 
    Rcpp::StringVector Id = x["ID"]; 
    Rcpp::StringVector NameId = x["NameID"]; 
    Rcpp::DateVector StDate = x["StartDate"]; 
    Rcpp::DateVector EtDate = x["EndDate"]; 

    /* 
    Rcpp::DateVector StDate_sub = StDate[ind]; 
    Rcpp::DateVector EtDate_sub = EtDate[ind]; 
    */ 

    return Rcpp::DataFrame::create(Rcpp::Named("string_1") = st1[ind], 
           Rcpp::Named("ID") = Id[ind], 
           Rcpp::Named("NameID") = NameId[ind]/*, 
           Rcpp::Named("StartDate") = StDate_sub, 
           Rcpp::Named("EndDate") = EtDate_sub*/ 
           ); 
}') 

내가받는 두 가지 주목할만한 오류가 있습니다. 미안, 내가 놓친거야. data.frame에 몇 가지 날짜 변수가 있습니다. 중첩 된 for 루프에서 데이터 집합의 하위 집합으로 Rcpp를 사용하고 있습니다. 현재 너무 많은 시간이 걸립니다. 서브셋 데이터 세트가 일부 처리 과정에서 필요하므로 data.table 또는 dplyr에 구현할 수 없습니다.

+1

하위 집합 연산자 구현은 새롭고 오래된'DateVector' 구현 모두에서 문제가되는 것처럼 보입니다. / – coatless

답변

2

먼저 정의 된 데이터 세트가 없으므로 예제가 최소한으로 재생산되지 않습니다.

둘째, 색인 벡터에 의한 지정이 날짜 벡터에 대해 정의된다는 (영웅적인) 가정을합니다. 나타나지 않을 수도 있습니다.

셋째, 루핑은 간단합니다. 아래 수정 된 코드. 참조 데이터가 제공되지 않았으므로 을 실행하는지는 알 수 없습니다..

#define RCPP_NEW_DATE_DATETIME_VECTORS 1 
#include <Rcpp.h> 

using namespace Rcpp; 

// [[Rcpp::export]] 
Rcpp::DataFrame dftest(DataFrame x, StringVector y) { 

    StringVector New = x["string_1"]; 
    std::string KEY = Rcpp::as<std::string>(y[0]); 
    Rcpp::LogicalVector ind(New.size()); 

    for(int i = 0; i < New.size(); i++){ 
    ind[i] = (New[i] == KEY); 
    } 


    Rcpp::StringVector st1 = x["string_1"]; 
    Rcpp::StringVector Id = x["ID"]; 
    Rcpp::StringVector NameId = x["NameID"]; 
    Rcpp::DateVector StDate = x["StartDate"]; 
    Rcpp::DateVector EtDate = x["EndDate"]; 

    int n = sum(ind); 
    Rcpp::DateVector StDate_sub = StDate(n); 
    Rcpp::DateVector EtDate_sub = EtDate(n); 
    for (int i=0; i<n; i++) { 
    StDate_sub[i] = StDate(ind[i]); 
    EtDate_sub[i] = EtDate(ind[i]); 
    } 

    return Rcpp::DataFrame::create(Rcpp::Named("string_1") = st1[ind], 
           Rcpp::Named("ID") = Id[ind], 
           Rcpp::Named("NameID") = NameId[ind], 
           Rcpp::Named("StartDate") = StDate_sub, 
           Rcpp::Named("EndDate") = EtDate_sub); 
}