-1
的RCPP子集行我希望在Rcpp
打造iris
數據集的這樣的一個子集:數據框中
head(subset(iris, Species == "versicolor"))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
53 6.9 3.1 4.9 1.5 versicolor
54 5.5 2.3 4.0 1.3 versicolor
55 6.5 2.8 4.6 1.5 versicolor
56 5.7 2.8 4.5 1.3 versicolor
我知道如何子集Rcpp::DataFrame
列 - 有重載運算符[
其工作方式在R: x["var"]
。但是,我找不到任何方法可以讓我將DataFrame的行分成不固定數量的列。
我想寫一個函數subset_rows_rcpp_iris
需要Rcpp::DataFrame
(它將永遠是光圈)和CharacterVector level_of_species
作爲輸入。它會返回DataFrame
對象。
DataFrame subset_rows_rcpp_iris(DataFrame x, CharacterVector level_of_species) {
...
}
首先,我想找到滿足邏輯查詢的行的索引。我的問題是,如果我在test
功能訪問Species
載體,將其保存爲CharacterVector
,然後將其與level_of_species
比較,我得到永遠只有一個TRUE
值的setosa
情況下,在其他情況下,FALSE值。
cppFunction('
LogicalVector test(DataFrame x, CharacterVector level_of_species) {
CharacterVector sub = x["Species"];
LogicalVector ind = sub == level_of_species;
return(ind);
}
')
head(test(iris, "setosa"))
[1] TRUE FALSE FALSE FALSE FALSE FALSE
如果這個工作,我可以重寫test
功能,並使用與載體TRUE/FALSE的值單獨子集中的每個數據幀的柱中,然後用Rcpp::DataFrame::create
再次將它們結合起來。
是的的確的。但是,我不知道如何在C++中表示字符標量。在Rcpp中沒有像'CharacterScalar'這樣的類。 '字符串'也不起作用。 –
對吧!我的錯誤..因爲有NumericScalar我想同樣在這裏....我認爲我們假設R在C++ recylcing在這裏做sub == level_of_species –
我們需要一個for循環 –