Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return NA if either sd in the denominator is 0. #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/sliding_cor_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ NumericVector sliding_cor_c(NumericVector shortvec, NumericVector longvec, doubl
int n = shortvec.size();
int n_minus1 = n - 1;
int out_length = length_longvec - n_minus1;
if (sd_shortvec < sqrt(DBL_EPSILON)) {
NumericVector out(out_length, NA_REAL);
return out;
}
NumericVector out(out_length);

// Calculate constant term to multiply all
Expand Down Expand Up @@ -37,7 +41,11 @@ NumericVector sliding_cor_c(NumericVector shortvec, NumericVector longvec, doubl
ss_longvec_current += pow(longvec_current_b - mean_longvec_current, 2);
}
double sd_longvec_current = sqrt(ss_longvec_current / n_minus1);
out[a] = (sum_products / n_minus1 - sum_longvec_current * term2) / sd_shortvec / sd_longvec_current;
if (sd_longvec_current < sqrt(DBL_EPSILON)) {
out[a] = NA_REAL;
} else {
out[a] = (sum_products / n_minus1 - sum_longvec_current * term2) / sd_shortvec / sd_longvec_current;
}
}
return out;
}
}