/
HandlingDataFrameExample2.cpp
164 lines (158 loc) · 4.61 KB
/
HandlingDataFrameExample2.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
SEXP ex10(DataFrame input){
CharacterVector names = input.names();
for(int i = 0; i<names.size(); i++){
cout << "names[" << i << "]=" << names[i] << "\n";
}
return(names);
}
// [[Rcpp::export]]
SEXP ex10_1(DataFrame input){
BEGIN_RCPP
CharacterVector names = input.names();
// vector<string> str_names = as<string>(names); // error
string str_names = as<string>(names); // names must have one element.
for(int i = 0; i<names.size(); i++){
cout << "names[" << i << "]=" << names[i] << "\n";
}
return(wrap(str_names));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex10_2(DataFrame input){
BEGIN_RCPP
List names = input.names();
for(int i = 0; i<names.size(); i++){
cout << "names[" << i << "]=" << as<string>(names[i]) << "\n"; }
return(wrap(names));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex10_3(DataFrame input){
BEGIN_RCPP
List names = input.names();
vector<string> strVec;
for(int i = 0; i<names.size(); i++){
string s = as<string>(names[i]);
strVec.push_back(s);
}
return(wrap(strVec));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex11(DataFrame input){
BEGIN_RCPP
List names = input.names();
map<string, int> mapObj;
for(int i = 0; i<names.size(); i++){
mapObj.insert(pair<string, int>(as<string>(names[i]),i));
}
return(wrap(mapObj));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex11_2(DataFrame input){
BEGIN_RCPP
List names = input.names();
map<string, NumericVector> mapObj;
for(int i = 0; i<names.size(); i++){
mapObj.insert(pair<string, NumericVector>(as<string>(names[i]),input(i)));
}
return(wrap(mapObj));
END_RCPP
}
void ex12helper(map<string, NumericVector> mapper, string columnName, int rowIndex, double replace){
(mapper[columnName])(rowIndex) = replace;
}
// [[Rcpp::export]]
SEXP ex12(DataFrame input, CharacterVector columnName, int rowIndex, double replace){
BEGIN_RCPP
List names = input.names();
map<string, NumericVector> mapObj;
for(int i = 0; i<names.size(); i++){
mapObj.insert(pair<string, NumericVector>(as<string>(names[i]),input(i)));
}
ex12helper(mapObj, as<string>(columnName), rowIndex, replace);
return(wrap(mapObj));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex12_2(DataFrame input, CharacterVector columnName, double replace){
BEGIN_RCPP
List names = input.names();
map<string, NumericVector> mapObj;
for(int i = 0; i<names.size(); i++){
mapObj.insert(pair<string, NumericVector>(as<string>(names[i]),input(i)));
}
// all rows
for(int i=0; i<input.nrows(); i++){
ex12helper(mapObj, as<string>(columnName), i, replace);
}
return(wrap(mapObj));
END_RCPP
}
// [[Rcpp::export]]
void ex13helper(List mapper, string columnName, int rowIndex, double replace){
as<NumericVector>(mapper(columnName))(rowIndex) = replace;
}
// [[Rcpp::export]]
SEXP ex13(DataFrame input, CharacterVector columnName, double replace){
BEGIN_RCPP
List names = input.names();
map<string, NumericVector> mapObj;
for(int i = 0; i<names.size(); i++){
mapObj.insert(pair<string, NumericVector>(as<string>(names[i]),input(i)));
}
// all rows
for(int i=0; i<input.nrows(); i++){
ex13helper(wrap(mapObj), as<string>(columnName), i, replace);
}
return(wrap(mapObj));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex13_2(DataFrame input, CharacterVector columnName, double replace){
BEGIN_RCPP
List names = input.names();
List mapObj = as<List>(input);
// all rows
for(int i=0; i<input.nrows(); i++){
ex13helper(mapObj, as<string>(columnName), i, replace);
}
return(wrap(mapObj));
END_RCPP
}
// [[Rcpp::export]]
SEXP ex14(DataFrame input, CharacterVector columnName, double replace){
BEGIN_RCPP
typedef List L;
L names = input.names();
L mapObj = as<L>(input);
// all rows
for(int i=0; i<input.nrows(); i++){
ex13helper(mapObj, as<string>(columnName), i, replace);
}
return(wrap(mapObj));
END_RCPP
}
/*** R
df1 <- data.frame(a=1)
df2 <- data.frame(a=1,b=2,aba=1.3, efa="tilder")
df3 <- data.frame(a=1:3,b=11:13,aba=c(1.3,1.4,8.2), efa=c("tilder", "hat", "camma"))
df12<- df3
a <- seq(from=1,to=1000000,by=1)
df12_2 <- data.frame(a=a, b=cumsum(a))
df13 <- data.frame(a=a, b=cumsum(a))
df13_2 <- data.frame(a=a, b=cumsum(a))
df14 <- data.frame(a=a,b=cumsum(a))
ex11(df1)
print(head(df12_2))
df12Returned <- data.frame(ex12(df12, "a", 1, replace=100.0))
system.time(df12_2Returned <- data.frame(ex12_2(df12_2, "a", replace=100.0)))
system.time(df13Returned <- data.frame(ex13(df13, "a", replace=100.0)))
system.time(df13_2Returned <- data.frame(ex13(df13_2, "a", replace=100.0)))
system.time(df14Returned <- data.frame(ex14(df14, "a", replace=100.0)))
*/