Chapter 21 R-like functions

Here is a list of Rcpp functions similar to R functions.

Also, if you can guarantee that NA is not included in the vector given to these functions, you can use noNA() to mark the vector. Then the functions below no longer checks for NA, so the calculation may speed up.

NumericVector res = mean(noNA(v));

21.1 List of R-like functions

21.1.3 Finding values

match(v, table)

Returns an integer vector containing the R style numerical index (starting from 1) of the element of vector table that match value to each elements of vector x. Namely、if you execute res = match(v, table);, then it will be res[i] == j+1 where j equals to minimum j satisfying v[i] == table[j]

self_match(v)

Synonymous with passing the same vector to match (v, table).

which_max(v)

Returns the numerical index of the largest element of the vector v.

which_min(v)

Returns the numerical index of the smallest element of the vector v.

21.1.4 Duplicated values

duplicated(v)

Returns a vector containing 1 if the value of each element of vector v exists in the previous element, containing 0 if not.

unique(v)

Returns a vector that eliminates the duplication of the element value from the vector v.

sort_unique(v)

Returns a vector that eliminates the duplication of the element value from the vector v and sorts the values in ascending order.

21.1.5 Set operation

setdiff(v1,v2)

Returns a vector obtained by subtracting the value of the unique element of the vector v2 from the unique element of the vector v1.

setequal(v1,v2)

Returns true if the unique element of vector v1 is equal to the unique element of vector v2.

intersect(v1,v2)

Returns a vector containing elements contained in both the unique element of vector v1 and the unique element of vector v2.

union_(v1,v2)

Return vector which eliminated value duplication after combining elements of vector v1 and vector v2.

21.1.12 apply functions

lapply(x, fun)

Applies a C++ function fun to each element of the vector x and returns the result as List.

sapply(x, fun)

Applies a C++ function fun to each element of the vector x and returns the result as Vector.

mapply(x1, x2, fun2)

Applies a C++ function fun that receives two arguments to each corresponding elements of the vector x1 and x2 and returns the result as Vector.

mapply(x1, x2, x3, fun3)

Applies a C++ function fun that receives three arguments to each corresponding elements of the vector x1 and x2, x3 and returns the result as Vector.

21.1.13 cbind function

cbind(x1, x2,…)

Takes Vector or Matrix x1 and x2, ... and combine by columns. And returns the result as Matrix or DataFrame. You can pass up to 50 arguments.

21.1.14 sampling

Vector sample(Vector x, int size, replace = false, probs = R_NilValue)

As with the sample function in R, this function takes a sample from a vector x.

  • x : a vector you want to draw a sample.
  • size : sample size of returned vector.
  • replace : should sampling be with replacement. default true.
  • probs : a vector that specify probability weights to be drawn for each elements of vector x. default R_NilValue (same weight for all elements).

Vector<RTYPE> sample(const Vector<RTYPE>& x, int size, bool replace = false, sugar::probs_t probs = R_NilValue)

sample(n, size, replace = TRUE, probs = NULL, one_based = TRUE)

As with the sample.int function in R.

Vector<INTSXP> sample(int n, int size, bool replace = false, sugar::probs_t probs = R_NilValue, bool one_based = true);