-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlistToMatrix.cpp
More file actions
70 lines (64 loc) · 2.17 KB
/
listToMatrix.cpp
File metadata and controls
70 lines (64 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <Rcpp.h>
#include <string>
using namespace Rcpp;
// [[Rcpp::export]]
CharacterMatrix listToMatrix(List data, List names) {
int rowCount = data.size(); // rows
int validColumnCount = names.size(); // name count
int indexColumnCount;
List indexList = *new List();
CharacterMatrix cMatrix = CharacterMatrix(rowCount, validColumnCount);
cMatrix.fill(NA_STRING);
// Get data indices of valid names. Faster to look up by direct access than searching list for name.
for(int c = 0; c < validColumnCount; c++)
{
indexList.push_back(as<List>(data[0]).findName(names[c]));
}
indexColumnCount = indexList.size();
// Loop through data
for(int i = 0; i < rowCount; i++)
{
// Loop through indices of valid names
for(int j = 0; j < indexColumnCount; j++)
{
// If values are not null add to matrix
if(!Rf_isNull((as<List>(data[i]))[as<int>(indexList[j])]))
{
// Issue 36837: GROUP CONCAT comes back as an array of values
if(TYPEOF((as<List>(data[i]))[as<int>(indexList[j])]) != VECSXP)
{
cMatrix(i,j) = (as<CharacterVector>((as<List>(as<List>(data[i]))[as<int>(indexList[j])])))[0];
}
else
{
// only try to parse the vector elements if the first element is not null
GenericVector gv = as<GenericVector>((as<List>(data[i]))[as<int>(indexList[j])]);
if(gv.size() > 0 && !Rf_isNull(gv[0]))
{
// Check if first element is a string - if so, concatenate all elements
if(TYPEOF(gv[0]) == STRSXP)
{
std::string result;
for(int k = 0; k < gv.size(); k++)
{
if(!Rf_isNull(gv[k]) && TYPEOF(gv[k]) == STRSXP)
{
String s = as<String>(gv[k]);
if(k > 0) result += ",";
result += s.get_cstring();
}
}
cMatrix(i,j) = result;
}
else
{
// Fallback to just returning the first element
cMatrix(i,j) = as<CharacterVector>(gv[0])[0];
}
}
}
}
}
}
return cMatrix;
}