1 package org.rosuda.REngine;
3 /** REXPFactor represents a factor in R. It is an integer vector with levels for each contained category. */
4 // FIXME: this is currently somehow screwed - the concept of RFactor and REXPFactor is duplicate - we need to remove this historical baggage
5 public class REXPFactor extends REXPInteger {
6 private String[] levels;
7 private RFactor factor;
9 /** create a new factor REXP
10 * @param ids indices (one-based!)
11 * @param levels levels */
12 public REXPFactor(int[] ids, String[] levels) {
14 this.levels = (levels==null)?(new String[0]):levels;
15 factor = new RFactor(this.payload, this.levels, false, 1);
19 new REXPString(this.levels), new REXPString("factor")
20 }, new String[] { "levels", "class" }));
23 /** create a new factor REXP
24 * @param ids indices (one-based!)
25 * @param levels levels
26 * @param attr attributes */
27 public REXPFactor(int[] ids, String[] levels, REXPList attr) {
29 this.levels = (levels==null)?(new String[0]):levels;
30 factor = new RFactor(this.payload, this.levels, false, 1);
33 /** create a new factor REXP from an existing RFactor
34 * @param factor factor object (can be of any index base, the contents will be pulled with base 1) */
35 public REXPFactor(RFactor factor) {
36 super(factor.asIntegers(1));
38 this.levels = factor.levels();
42 new REXPString(this.levels), new REXPString("factor")
43 }, new String[] { "levels", "class" }));
46 /** create a new factor REXP from an existing RFactor
47 * @param factor factor object (can be of any index base, the contents will be pulled with base 1)
48 * @param attr attributes */
49 public REXPFactor(RFactor factor, REXPList attr) {
50 super(factor.asIntegers(1), attr);
52 this.levels = factor.levels();
55 public boolean isFactor() { return true; }
57 /** return the contents as a factor - the factor is guaranteed to have index base 1
58 * @return the contents as a factor */
59 public RFactor asFactor() {
63 public String[] asStrings() {
64 return factor.asStrings();
67 public Object asNativeJavaObject() {
71 public String toString() {
72 return super.toString()+"["+levels.length+"]";