[英]remove NA from NumericVector in RCpp
I am a newbie to Rcpp (and a newbie to C++). 我是Rcpp的新手(和C ++的新手)。 I wrote a code using an example code here https://gist.github.com/kevinushey/4561281 to calculate row/col max/mix for matrices as follows:
我在这里https://gist.github.com/kevinushey/4561281使用示例代码编写了一个代码,用于计算矩阵的row / col max / mix,如下所示:
#include <Rcpp.h>
using namespace Rcpp;
template <class T>
inline double do_max(T& x) {
return max(x);
}
template <class T>
inline double do_min(T& x) {
return min(x);
}
NumericVector colMaxsCpp(NumericMatrix& x) {
int nRows = x.nrow();
NumericVector out = no_init(nRows);
for (int i=0; i<nRows;i++) {
NumericMatrix::Row tmp = x(i,_);
out[i]=do_max(tmp);
}
return out;
}
NumericVector colMinsCpp(NumericMatrix& x) {
int nRows = x.nrow();
NumericVector out = no_init(nRows);
for (int i=0; i<nRows;i++) {
NumericMatrix::Row tmp = x(i,_);
out[i]=do_min(tmp);
}
return out;
}
NumericVector rowMaxsCpp(NumericMatrix& x) {
int nCols = x.ncol();
NumericVector out = no_init(nCols);
for (int i=0; i<nCols;i++) {
NumericMatrix::Column tmp = x(_,i);
out[i]=do_max(tmp);
}
return out;
}
NumericVector rowMinsCpp(NumericMatrix& x) {
int nCols = x.ncol();
NumericVector out = no_init(nCols);
for (int i=0; i<nCols;i++) {
NumericMatrix::Column tmp = x(_,i);
out[i]=do_min(tmp);
}
return out;
}
// [[Rcpp::export]]
NumericVector Max(NumericMatrix x, int dim) {
if (dim==1) {
return rowMaxsCpp(x);
} else if (dim==2) {
return colMaxsCpp(x);
}
}
// [[Rcpp::export]]
NumericVector Min(NumericMatrix x, int dim) {
if (dim==1) {
return rowMinsCpp(x);
} else if (dim==2) {
return colMinsCpp(x);
}
}
I want to modify to code to handle NA
in the do_min
and do_max
functions. 我想修改代码以处理
do_min
和do_max
函数中的NA
。 The do_min
and do_max
functions use a template
of a class. do_min
和do_max
函数使用类的template
。
I read on the web about NA_REAL
and NA_INTEGER
but these are specific to classes. 我在网络上阅读了有关
NA_REAL
和NA_INTEGER
但它们特定于类。
(a) Is there any generic NA
available for a template
of a class (such as in the example above)? (a)是否有可用于类
template
的通用NA
(例如在上面的示例中)?
(b) Also, is there any function available that I can subset a variable x
to only use non- NA
elements, eg equivalent of max(x[!is.na(x)])
or min(x[!is.na(x)])
in R
? (b)另外,有没有可用的函数可以对变量
x
进行子集以仅使用非NA
元素,例如max(x[!is.na(x)])
或min(x[!is.na(x)])
在R
?
(c) Finally, the code above has been written to handle a numeric matrix
, but it seems to work even when an integer matrix
has been supplied (albeit the output is converted to numeric
). (c)最后,上面的代码已编写为处理数字
matrix
,但即使提供了整数matrix
(即使输出转换为numeric
),它也似乎可以工作。 Why is this so? 为什么会这样呢?
Quick ones: 快速的:
a) Yes, there are but I am not sure they are vectorised. a)是的,但是我不确定它们是矢量化的。 But some templated NA traits were added.
但是增加了一些模板化的NA特性。
b) Yes as eg in b)是,例如
R> cppFunction('double mymax(NumericVector x) { \
IntegerVector x2 = wrap(na_omit(x)); \
return max(x2);}')
R> mymax(c(1L, 2L, NA, 4L))
[1] 4
R>
c) Integer always gets 'cast up' to Numeric at the cost of a copy. c)整数总是以复制为代价“转换”为数字。
@Roland already hinted at na_omit
. @Roland已经暗示了
na_omit
。 In b), somehow I need to help with a wrap()
to generate an intermediate SEXP
object. 在b)中,我需要某种方式来帮助
wrap()
来生成中间SEXP
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.