Skip to content

Instantly share code, notes, and snippets.

@kenahoo
Last active December 13, 2015 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenahoo/4991485 to your computer and use it in GitHub Desktop.
Save kenahoo/4991485 to your computer and use it in GitHub Desktop.
Rcpp for DataFrame access
library(Rcpp)
cppFunction('
int countSteps (DataFrame df, double thresh=0.001) {
int out = 0;
if(df.nrows()==0) return out;
int last_i = 0;
out++;
NumericVector xs = df[0];
NumericVector ys = df[1];
for(int i=1; i<xs.length(); i++) {
if(fabs(xs[i]-xs[last_i])>thresh ||
fabs(ys[i]-ys[last_i])>thresh) {
out++;
last_i = i;
}
}
return out;
}')
cppFunction('
int countSteps2 (DataFrame df, double thresh=0.001) {
int out = 0;
int n = df.nrows();
if (n==0) return out;
int last_i = 0;
out++;
int m = df.length();
for(int i=1; i<n; i++) {
for(int j=0; j<m; j++) {
const NumericVector& col_j = df[j];
if(fabs(col_j[i]-col_j[last_i])>thresh) {
out++;
last_i = i;
break;
}
}
}
return out;
}')
cppFunction('
int countSteps3 (NumericMatrix nm, double thresh=0.001) {
int out = 0;
int n = nm.nrow();
if (n==0) return out;
int last_i = 0;
out++;
int m = nm.ncol();
for(int i=1; i<n; i++) {
for(int j=0; j<m; j++) {
NumericMatrix::Column col_j = nm(_,j);
if(fabs(col_j[i]-col_j[last_i])>thresh) {
out++;
last_i = i;
break;
}
}
}
return out;
}')
cppFunction('
int countSteps4 (DataFrame df, double thresh=0.001) {
int out = 0;
int n = df.nrows();
if (n==0) return out;
int last_i = 0;
out++;
int m = df.length();
NumericVector cols[m];
for(int j=0; j<m; j++) {
cols[j] = df[j];
}
for(int i=1; i<n; i++) {
for(int j=0; j<m; j++) {
if(fabs(cols[j][i]-cols[j][last_i])>thresh) {
out++;
last_i = i;
break;
}
}
}
return out;
}')
df <- data.frame(x=cumsum(rnorm(2e7)), y=cumsum(rnorm(2e7)))
print(system.time(x <- countSteps(df,3)))
print(system.time(x2 <- countSteps2(df,3)))
print(system.time(x3 <- countSteps3(data.matrix(df),3)))
print(system.time(x4 <- countSteps4(df,3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment