Rcpp: Excpecting Single Value

I've got two files: main.R and mv.cpp.




r <- data.frame(replicate(10, sample(0:10, 10000, replace = TRUE)))


and mv.cpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void mv(Rcpp::DataFrame x){
int n = 0;
int nrow = x.nrow();
int ncol = x.ncol();
int mv = 0;

  for (int t =0; t < 10 + 0*nrow; t++){
     mv = x[0, t];
     for (int d = 0; d < ncol; d++){
           if (NumericVector::is_na(x[d,t])){
             x[d,t ] = mv;
          } else {
               mv = x[d, t];


I compile mv.cpp and then run main.R and get the error message:
Error in mv(r) : Expecting a single value: [extent=10000].

I've tried changing mv.cpp from void to int and still get the same error.

you aren't returning any expression. did you intend to return the mv that you calculate within the loop ?

I want to replace missing values with the previous valid value. Each column is a variable and each row is a sequential observation. I made it "void mv()" to make it as simple as possible and to isolate the problem.
Thanks for your interest.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.