SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.misc
Class R

java.lang.Object
  extended by com.numericalmethod.suanshu.misc.R

public class R
extends java.lang.Object

This class collects some R-equivalent utility functions. Most of the R-equivalent functions are implemented as classes, e.g., Rank. The functions listed here are the commonly used utility function rather than mathematical concepts.

We try to use the same names and syntax whenever possible as in R to allow easier conversion from R code.

See Also:
The R Project for Statistical Computing

Nested Class Summary
static interface R.ifelse
          the placeholder for R.ifelse
static interface R.which
          the placeholder for R.which
 
Method Summary
static double[] cumsum(double[] arr)
          Get an array whose elements are the cumulative sum of the elements of the input array.
static int[] cumsum(int[] arr)
          Get an array whose elements are the cumulative sum of the elements of the input array.
static double[] diff(double[] arr)
          Get the first differences of an array.
static double[][] diff(double[][] arr)
          Get the first differences of an array of vectors.
static double[][] diff(double[][] arr, int lag, int difference)
          Get the lagged and iterated differences of vectors.
static double[] diff(double[] arr, int lag, int difference)
          Get the lagged and iterated differences.
static double[] ifelse(double[] arr, R.ifelse selection)
          Get an array with the same length as arr which is filled with either "yes" or "no" values according to the boolean/binary classification in R.ifelse.
static int[] order(double[] arr)
          Get a permutation which rearranges an array into ascending order.
static int[] order(double[] arr, boolean ascending)
          Get a permutation which rearranges an array into ascending or descending order.
static java.lang.String paste(java.util.AbstractCollection<java.lang.String> collection, java.lang.String delimiter)
          Concatenate Strings into one String.
static double[] rep(double value, int times)
          This generates an array of doubles of repeated values.
static int[] rep(int value, int times)
          This generates an array of ints of repeated values.
static double[] select(double[] arr, R.which criterion)
          Select the array elements which satisfy the boolean criterion.
static int[] select(int[] arr, R.which criterion)
          Select the array elements which satisfy the boolean criterion.
static double[] seq(double from, double to, double inc)
          This generates a sequence of doubles from from up to to with increments inc.
static int[] seq(int from, int to)
          This generates a sequence of ints from from to to with increments 1.
static int[] seq(int from, int to, int inc)
          This generates a sequence of doubles from from up to to with increments inc.
static double[] subarray(double[] arr, int[] indices)
          Get a subarray of the original array with the given indices.
static int[] subarray(int[] arr, int[] indices)
          Get a subarray of the original array with the given indices.
static int[] which(double[] arr, R.which criterion)
          Get the indices of the array elements which satisfy the boolean criterion.
static int[] which(int[] arr, R.which criterion)
          Get the indices of the array elements which satisfy the boolean criterion.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

rep

public static double[] rep(double value,
                           int times)
This generates an array of doubles of repeated values.

Parameters:
value - the repeated value
times - the length of the array
Returns:
an array of repetitions of value

rep

public static int[] rep(int value,
                        int times)
This generates an array of ints of repeated values.

Parameters:
value - the repeated value
times - the length of the array
Returns:
an array of repetitions of value

seq

public static double[] seq(double from,
                           double to,
                           double inc)
This generates a sequence of doubles from from up to to with increments inc.

The last number in the sequence is smaller than or equal to to for positive inc. The last number in the sequence is bigger than or equal to to for negative inc.

Parameters:
from - the first number in the sequence
to - the bound of the sequence
inc - the increment
Returns:
a sequence of doubles

seq

public static int[] seq(int from,
                        int to,
                        int inc)
This generates a sequence of doubles from from up to to with increments inc.

The last number in the sequence is smaller than or equal to to for positive inc. The last number in the sequence is bigger than or equal to to for negative inc.

Parameters:
from - the first number in the sequence
to - the bound of the sequence
inc - the increment
Returns:
a sequence of ints

seq

public static int[] seq(int from,
                        int to)
This generates a sequence of ints from from to to with increments 1. That is, [from, to], inclusively.

Parameters:
from - the first number in the sequence
to - the bound of the sequence
Returns:
a sequence of ints

diff

public static double[] diff(double[] arr,
                            int lag,
                            int difference)
Get the lagged and iterated differences.

Parameters:
arr - a double[] array
lag - an integer indicating which lag to use
difference - an integer indicating the order of the difference. This is the number of times 'diff' is applied to the data. E.g., diff(x, 1, 2) = diff(diff(x, 1, 1), 1, 1).
Returns:
diff(arr, lag, difference)

diff

public static double[] diff(double[] arr)
Get the first differences of an array.

Parameters:
arr - a double[] array
Returns:
diff(arr)

diff

public static double[][] diff(double[][] arr,
                              int lag,
                              int difference)
Get the lagged and iterated differences of vectors.

Parameters:
arr - a double[][] array; row view; cannot be jagged
lag - an integer indicating which lag to use
difference - an integer indicating the order of the difference. This is the number of times 'diff' is applied to the data. E.g., diff(x, 1, 2) = diff(diff(x, 1, 1), 1, 1).
Returns:
diff(arr, lag, difference)

diff

public static double[][] diff(double[][] arr)
Get the first differences of an array of vectors.

Parameters:
arr - a double[][] array; row view; cannot be jagged
Returns:
diff(arr)

cumsum

public static double[] cumsum(double[] arr)
Get an array whose elements are the cumulative sum of the elements of the input array.

Parameters:
arr - a double[] array
Returns:
cumsum(arr)

cumsum

public static int[] cumsum(int[] arr)
Get an array whose elements are the cumulative sum of the elements of the input array.

Parameters:
arr - a int[] array
Returns:
cumsum(arr)

ifelse

public static double[] ifelse(double[] arr,
                              R.ifelse selection)
Get an array with the same length as arr which is filled with either "yes" or "no" values according to the boolean/binary classification in R.ifelse.

Parameters:
arr - an input double[] array
selection - the classification criterion to decide true or false for a number
Returns:
ifelse values

which

public static int[] which(double[] arr,
                          R.which criterion)
Get the indices of the array elements which satisfy the boolean criterion.

Parameters:
arr - a double[] array
criterion - determines which elements to select
Returns:
the indices of the satisfying elements

which

public static int[] which(int[] arr,
                          R.which criterion)
Get the indices of the array elements which satisfy the boolean criterion.

Parameters:
arr - a double[] array
criterion - determines which elements to select
Returns:
the indices of the satisfying elements

select

public static double[] select(double[] arr,
                              R.which criterion)
Select the array elements which satisfy the boolean criterion.

R does not have a 'select' function. It is equivalent to: arr[which(...}]

Parameters:
arr - a double[] array
criterion - determines which elements to select
Returns:
the the satisfying elements

select

public static int[] select(int[] arr,
                           R.which criterion)
Select the array elements which satisfy the boolean criterion.

R does not have a 'select' function. It is equivalent to: arr[which(...}]

Parameters:
arr - a double[] array
criterion - determines which elements to select
Returns:
the the satisfying elements

subarray

public static double[] subarray(double[] arr,
                                int[] indices)
Get a subarray of the original array with the given indices. The R-equivalent is arr[indices].

Parameters:
arr - a double[] array
indices - an array of indices to select
Returns:
arr[indices]

subarray

public static int[] subarray(int[] arr,
                             int[] indices)
Get a subarray of the original array with the given indices. The R-equivalent is arr[indices].

Parameters:
arr - a int[] array
indices - an array of indices to select
Returns:
arr[indices]

order

public static int[] order(double[] arr,
                          boolean ascending)
Get a permutation which rearranges an array into ascending or descending order.

Parameters:
arr - a double[] array
ascending - true if arranging elements in ascending order; false if descending order
Returns:
a permutation that is counting from 0

order

public static int[] order(double[] arr)
Get a permutation which rearranges an array into ascending order.

Parameters:
arr - a double[] array
Returns:
a permutation that is counting from 0

paste

public static java.lang.String paste(java.util.AbstractCollection<java.lang.String> collection,
                                     java.lang.String delimiter)
Concatenate Strings into one String.

Parameters:
collection - the set of strings to be concatenated
delimiter - the separation symbol for the strings
Returns:
the concatenated String

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.