Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(plyr)
- library(dplyr)
- library(purrr)
- library(magrittr)
- library(Rcpp)
- sourceCpp(code = '
- #include <Rcpp.h>
- #include <string>
- #include <vector>
- using namespace Rcpp;
- using namespace std;
- NumericVector add_narm(NumericVector x, NumericVector y) {
- NumericVector out(x.size());
- NumericVector::iterator x_it,y_it,out_it;
- for (x_it = x.begin(), y_it=y.begin(), out_it = out.begin();
- x_it != x.end(); ++x_it, ++y_it, ++out_it) {
- if (ISNA(*x_it)) {
- *out_it = *y_it;
- } else if (ISNA(*y_it)) {
- *out_it = *x_it;
- } else {
- *out_it = *x_it + *y_it;
- }
- }
- return out;
- }
- // [[Rcpp::export]]
- DataFrame merge_all_cpp(List dfs){
- DataFrame out(dfs[0]);
- vector<string> df_names = out.names();
- for (int i = 1; i < dfs.length(); i++)
- {
- DataFrame tmp(dfs[i]);
- vector<string> tmp_names = tmp.names();
- for (int j = 0; j < tmp.length(); j++)
- {
- if (find(df_names.begin(), df_names.end(), tmp_names[j]) != df_names.end())
- {
- out[tmp_names[j]] = add_narm(
- as<NumericVector>(out[tmp_names[j]]),
- as<NumericVector>(tmp[tmp_names[j]]));
- }
- else
- {
- out.push_back(tmp[tmp_names[j]], tmp_names[j]);
- df_names.push_back(tmp_names[j]);
- }
- }
- }
- return out;
- }');
- t = proc.time()
- dfs = list(w,x,y,z)
- by = "SP"
- overall_keys = list(w,x,y,z) %>% map(~select_(., .dots = by)) %>% rbindlist %>% distinct
- dfs %<>% map(~merge(., overall_keys, by = by, all = TRUE))
- ## not use purrr
- # overall_keys = dfs %>% lapply(function(x) select_(x, .dots = by)) %>% rbindlist %>% distinct
- # dfs %<>% lapply(function(x) merge(x, overall_keys, by = by, all = TRUE))
- f = function(x) x / length(dfs)
- out7 = merge_all_cpp(dfs) %>% tbl_dt(FALSE) %>% mutate_each_(funs(f), by)
- proc.time() - t
- # user system elapsed
- # 1.46 0.11 1.57
- out7 %<>% select_(.dots = names(out4)) %>%
- arrange_(.dots = by) %>% mutate_each(funs(as.integer))
- all.equal(out4, out7) # TRUE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement