R
,
Data <- c( 4 ,5 ,6, 7, 8)
index <- matrix(c(1,2,3, 1, 0, 0, 2,0,2), byrow = TRUE, nrow = 3)
apply(index, 1, function(x) Data[x])
index <- matrix(c(1,2,3, 1, 0, 0, 2,0,2), byrow = TRUE, nrow = 3)
index[index == 0] <- NA
index
apply(index, 2, function(x) Data[x])
[
:
matrix( Data[index], nrow = 3, byrow = FALSE)
Rcpp: 0 NA Data
vector
apply
Rcpp
,
[
: article RCpp
: mysubset.cpp
#include <Rcpp.h>
using namespace Rcpp;
NumericVector mysubset(NumericVector Data, NumericVector index) {
return Data[index];
}
RStudio:
library('Rcpp')
sourceCpp("mysubset.cpp")
Data <- c( NA, 4 ,5 ,6, 7, 8)
index <- matrix(c(1,2,3, 1, 0, 0, 2,0,2), byrow = TRUE, nrow = 3)
matrix( mysubset(Data, index), nrow = 3, byrow = FALSE)
mysubset(Data, index[1,])
na.omit(mysubset(Data, index[2,]))