好的,這是我能想到的一種方法。
首先,我們真的不能在Rcpp
中使用Rcpp::DataFrame
對象類型,因爲它確實是一個鬆散的向量列表。所以,我通過創建與採樣數據相匹配的Rcpp::NumericMatrix
來降低此問題的閾值。從這裏開始,可以使用std::map
來計算唯一的行數。這是簡化的,因爲Rcpp::NumericMatrix
具有啓用子集的.row()
屬性。所以,每一行然後被轉換爲一個std::vector<T>
,它被用作地圖的關鍵字。然後,我們將每個std::vector<T>
添加到std::map
並增加其計數值。最後,我們將std::map
導出爲所需的矩陣格式。
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericMatrix unique_rows(Rcpp::NumericMatrix & v)
{
// Initialize a map
std::map<std::vector<double>, int> count_rows;
// Clear map
count_rows.clear();
// Count each element
for (int i = 0; i != v.nrow(); ++i) {
// Pop from R Matrix
Rcpp::NumericVector a = v.row(i);
// Convert R vector to STD vector
std::vector<double> b = Rcpp::as< std::vector<double> >(a);
// Add to map
count_rows[ b ] += 1;
}
// Make output matrix
Rcpp::NumericMatrix o(count_rows.size(), v.ncol()+1);
// Hold count iteration
unsigned int count = 0;
// Start at the 1st element and move to the last element in the map.
for(std::map<std::vector<double>,int>::iterator it = count_rows.begin();
it != count_rows.end(); ++it)
{
// Grab the key of the matrix
std::vector<double> temp_o = it->first;
// Tack on the vector, probably can be speed up.
temp_o.push_back(it->second);
// Convert from std::vector to Rcpp::NumericVector
Rcpp::NumericVector mm = Rcpp::wrap(temp_o);
// Store in a NumericMatrix
o.row(count) = mm;
count++;
}
return o;
}
然後我們一起去:
a = matrix(c(1, 1, 1, 1, 2,
1, 1, 1, 1, 2,
2, 2, 2, 2, 3,
2, 2, 2, 2, 3,
3, 3, 3, 3, 1), ncol = 5, byrow = T)
unique_rows(a)
,並提供:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 2 2
[2,] 2 2 2 2 3 2
[3,] 3 3 3 3 1 1
[這](http://stackoverflow.com/questions/18201074/find-how-many-times -duplicated-rows-repeat-in-r-data-frame)答案可能會有所幫助。 –