[Rcpp-commits] r4309 - in pkg/Rcpp: . inst inst/include/Rcpp/sugar/functions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Apr 16 11:52:35 CEST 2013


Author: romain
Date: 2013-04-16 11:52:35 +0200 (Tue, 16 Apr 2013)
New Revision: 4309

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/na_omit.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
Log:
added na_omit

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-04-14 07:50:55 UTC (rev 4308)
+++ pkg/Rcpp/ChangeLog	2013-04-16 09:52:35 UTC (rev 4309)
@@ -1,3 +1,8 @@
+2013-04-16  Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/sugar/functions/na_omit.h : new function na_omit to remove
+        missing values from a vector. 
+
 2013-04-14  Romain Francois <romain at r-enthusiasts.com>
 
         * include/Rcpp/traits/is_na.h: complex version did not work. 

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2013-04-14 07:50:55 UTC (rev 4308)
+++ pkg/Rcpp/inst/NEWS.Rd	2013-04-16 09:52:35 UTC (rev 4309)
@@ -14,6 +14,11 @@
       \item The \code{operator-()} semantics for \code{Date} and
       \code{Datetime} are now more inline with standard C++ behaviour.
     }
+    \item Changes in sugar:
+    \itemize{
+        \item New function \code{na_omit} based on the stack overflow thread
+        http://stackoverflow.com/questions/15953768/templated-rcpp-function-to-erase-na-values
+    }
 }
 
 \section{Changes in Rcpp version 0.10.3 (2013-03-23)}{

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-04-14 07:50:55 UTC (rev 4308)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-04-16 09:52:35 UTC (rev 4309)
@@ -2,7 +2,7 @@
 //
 // functions.h: Rcpp R/C++ interface class library -- sugar functions
 //
-// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -34,6 +34,7 @@
 #include <Rcpp/sugar/functions/any.h>
 #include <Rcpp/sugar/functions/all.h>
 #include <Rcpp/sugar/functions/is_na.h>
+#include <Rcpp/sugar/functions/na_omit.h>
 #include <Rcpp/sugar/functions/seq_along.h>
 #include <Rcpp/sugar/functions/sapply.h>
 #include <Rcpp/sugar/functions/mapply.h>

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/na_omit.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/na_omit.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/na_omit.h	2013-04-16 09:52:35 UTC (rev 4309)
@@ -0,0 +1,79 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// na_omit.h: Rcpp R/C++ interface class library -- na_omit
+//
+// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__sugar__na_omit_h
+#define Rcpp__sugar__na_omit_h
+
+namespace Rcpp{
+namespace sugar{
+     
+    template <int RTYPE, bool NA, typename T>
+    Vector<RTYPE> na_omit_impl(const T& x, Rcpp::traits::false_type ) {
+        int n = x.size() ;
+        int n_out = n - sum( is_na(x) ) ;
+    
+        Vector<RTYPE> out(n_out) ;
+        for( int i=0, j=0; i<n; i++){
+            if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
+            out[j++] = x[i];
+        }
+        return out ;
+    }  
+
+    template <int RTYPE, bool NA, typename T>
+    Vector<RTYPE> na_omit_impl(const T& x, Rcpp::traits::true_type ) {
+        int n = x.size() ;
+        int n_out = n - sum( is_na(x) ) ;
+    
+        Vector<RTYPE> out(n_out) ;
+        bool has_name = x.attr("names") != R_NilValue ;
+        if( has_name ){
+            CharacterVector names = x.attr("names") ;
+            CharacterVector onames( n_out ) ;
+            
+            for( int i=0, j=0; i<n; i++){
+                if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
+                onames[j] = names[i] ;
+                out[j++] = x[i];
+            }
+            out.attr("names") = onames ;
+        } else {
+            for( int i=0, j=0; i<n; i++){
+                if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
+                out[j++] = x[i];
+            }
+        }
+        return out ;
+    }  
+    
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+inline Vector<RTYPE> na_omit( const VectorBase<RTYPE,NA,T>& t){
+	return sugar::na_omit_impl<RTYPE,NA,T>( 
+	    t.get_ref(), 
+	    typename Rcpp::traits::same_type<T,Vector<RTYPE> >::type()
+	) ;
+}
+
+} // Rcpp
+#endif
+



More information about the Rcpp-commits mailing list