/** returns the contents as an array of doubles (if supported by the represented object) */ public double[] asDoubles() throws REXPMismatchException { throw new REXPMismatchException(this, "double"); } /** returns the contents as an array of bytes (if supported by the represented object) */
/** returns the contents as a (named) list (if supported by the represented object) */ public RList asList() throws REXPMismatchException { throw new REXPMismatchException(this, "list"); } /** returns the contents as a factor (if supported by the represented object) */
/** returns a boolean vector of the same length as this vector with <code>true</code> for NA values and <code>false</code> for any other values * @return a boolean vector of the same length as this vector with <code>true</code> for NA values and <code>false</code> for any other values * @throws REXPMismatchException if this is not a vector object */ public boolean[] isNA() throws REXPMismatchException { throw new REXPMismatchException(this, "vector"); }
/** returns the length of a vector object. Note that we use R semantics here, i.e. a matrix will have a length of <i>m * n</i> since it is represented by a single vector (see {@link #dim} for retrieving matrix and multidimentional-array dimensions). * @return length (number of elements) in a vector object * @throws REXPMismatchException if this is not a vector object */ public int length() throws REXPMismatchException { throw new REXPMismatchException(this, "vector"); }
/** returns the contents as a (named) list (if supported by the represented object) */ public RList asList() throws REXPMismatchException { throw new REXPMismatchException(this, "list"); } /** returns the contents as a factor (if supported by the represented object) */
/** returns the contents as an array of Strings (if supported by the represented object) */ public String[] asStrings() throws REXPMismatchException { throw new REXPMismatchException(this, "String"); } /** returns the contents as an array of integers (if supported by the represented object) */
/** returns the contents as a factor (if supported by the represented object) */ public RFactor asFactor() throws REXPMismatchException { throw new REXPMismatchException(this, "factor"); } /** attempt to represent the REXP by a native Java object and return it. Note that this may lead to loss of information (e.g., factors may be returned as a string array) and attributes are ignored. Not all R types can be converted to native Java objects. Also note that R has no concept of scalars, so vectors of length 1 will always be returned as an arrays (i.e., <code>int[1]</code> and not <code>Integer</code>). */
/** returns the contents as an array of bytes (if supported by the represented object) */ public byte[] asBytes() throws REXPMismatchException { throw new REXPMismatchException(this, "byte"); } /** returns the contents as a (named) list (if supported by the represented object) */
/** returns the contents as an array of integers (if supported by the represented object) */ public int[] asIntegers() throws REXPMismatchException { throw new REXPMismatchException(this, "int"); } /** returns the contents as an array of doubles (if supported by the represented object) */
/** returns the length of a vector object. Note that we use R semantics here, i.e. a matrix will have a length of <i>m * n</i> since it is represented by a single vector (see {@link #dim} for retrieving matrix and multidimentional-array dimensions). * @return length (number of elements) in a vector object * @throws REXPMismatchException if this is not a vector object */ public int length() throws REXPMismatchException { throw new REXPMismatchException(this, "vector"); }
/** returns a boolean vector of the same length as this vector with <code>true</code> for NA values and <code>false</code> for any other values * @return a boolean vector of the same length as this vector with <code>true</code> for NA values and <code>false</code> for any other values * @throws REXPMismatchException if this is not a vector object */ public boolean[] isNA() throws REXPMismatchException { throw new REXPMismatchException(this, "vector"); }
/** returns the contents as an array of doubles (if supported by the represented object) */ public double[] asDoubles() throws REXPMismatchException { throw new REXPMismatchException(this, "double"); } /** returns the contents as an array of bytes (if supported by the represented object) */
/** returns the contents as an array of bytes (if supported by the represented object) */ public byte[] asBytes() throws REXPMismatchException { throw new REXPMismatchException(this, "byte"); } /** returns the contents as a (named) list (if supported by the represented object) */
/** returns the contents as an array of Strings (if supported by the represented object) */ public String[] asStrings() throws REXPMismatchException { throw new REXPMismatchException(this, "String"); } /** returns the contents as an array of integers (if supported by the represented object) */
/** returns the contents as an array of integers (if supported by the represented object) */ public int[] asIntegers() throws REXPMismatchException { throw new REXPMismatchException(this, "int"); } /** returns the contents as an array of doubles (if supported by the represented object) */
/** returns the contents as a factor (if supported by the represented object) */ public RFactor asFactor() throws REXPMismatchException { throw new REXPMismatchException(this, "factor"); } /** attempt to represent the REXP by a native Java object and return it. Note that this may lead to loss of information (e.g., factors may be returned as a string array) and attributes are ignored. Not all R types can be converted to native Java objects. Also note that R has no concept of scalars, so vectors of length 1 will always be returned as an arrays (i.e., <code>int[1]</code> and not <code>Integer</code>). */
/** attempt to represent the REXP by a native Java object and return it. Note that this may lead to loss of information (e.g., factors may be returned as a string array) and attributes are ignored. Not all R types can be converted to native Java objects. Also note that R has no concept of scalars, so vectors of length 1 will always be returned as an arrays (i.e., <code>int[1]</code> and not <code>Integer</code>). */ public Object asNativeJavaObject() throws REXPMismatchException { throw new REXPMismatchException(this, "native Java Object"); }
/** attempt to represent the REXP by a native Java object and return it. Note that this may lead to loss of information (e.g., factors may be returned as a string array) and attributes are ignored. Not all R types can be converted to native Java objects. Also note that R has no concept of scalars, so vectors of length 1 will always be returned as an arrays (i.e., <code>int[1]</code> and not <code>Integer</code>). */ public Object asNativeJavaObject() throws REXPMismatchException { throw new REXPMismatchException(this, "native Java Object"); }
/** returns the content of the REXP as a matrix of doubles (2D-array: m[rows][cols]). This is the same form as used by popular math packages for Java, such as JAMA. This means that following leads to desired results:<br> <code>Matrix m=new Matrix(c.eval("matrix(c(1,2,3,4,5,6),2,3)").asDoubleMatrix());</code><br> @return 2D array of doubles in the form double[rows][cols] or <code>null</code> if the contents is no 2-dimensional matrix of doubles */ public double[][] asDoubleMatrix() throws REXPMismatchException { double[] ct = asDoubles(); REXP dim = getAttribute("dim"); if (dim == null) throw new REXPMismatchException(this, "matrix (dim attribute missing)"); int[] ds = dim.asIntegers(); if (ds.length != 2) throw new REXPMismatchException(this, "matrix (wrong dimensionality)"); int m = ds[0], n = ds[1]; double[][] r = new double[m][n]; // R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4) // we need to copy everything, since we create 2d array from 1d array int k = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) r[j][i] = ct[k++]; return r; }
/** creates a data frame object from a list object using integer row names * @param l a (named) list of vectors ({@link REXPVector} subclasses), each element corresponds to a column and all elements must have the same length * @return a data frame object * @throws REXPMismatchException if the list is empty or any of the elements is not a vector */ public static REXP createDataFrame(RList l) throws REXPMismatchException { if (l == null || l.size() < 1) throw new REXPMismatchException(new REXPList(l), "data frame (must have dim>0)"); if (!(l.at(0) instanceof REXPVector)) throw new REXPMismatchException(new REXPList(l), "data frame (contents must be vectors)"); REXPVector fe = (REXPVector) l.at(0); return new REXPGenericVector(l, new REXPList( new RList( new REXP[] { new REXPString("data.frame"), new REXPString(l.keys()), new REXPInteger(new int[] { REXPInteger.NA, -fe.length() }) }, new String[] { "class", "names", "row.names" }))); }