]> gerrit.simantics Code Review - simantics/r.git/blob - bundles/org.simantics.r.scl/src/org/rosuda/REngine/REXPFactor.java
Restructured R repository for Tycho POMless builds.
[simantics/r.git] / bundles / org.simantics.r.scl / src / org / rosuda / REngine / REXPFactor.java
1 package org.rosuda.REngine;
2
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;
8         
9         /** create a new factor REXP
10          *  @param ids indices (one-based!)
11          *  @param levels levels */
12         public REXPFactor(int[] ids, String[] levels) {
13                 super(ids);
14                 this.levels = (levels==null)?(new String[0]):levels;
15                 factor = new RFactor(this.payload, this.levels, false, 1);
16                 attr = new REXPList(
17                                                         new RList(
18                                                                           new REXP[] {
19                                                                                   new REXPString(this.levels), new REXPString("factor")
20                                                                           }, new String[] { "levels", "class" }));
21         }
22
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) {
28                 super(ids, attr);
29                 this.levels = (levels==null)?(new String[0]):levels;
30                 factor = new RFactor(this.payload, this.levels, false, 1);
31         }
32         
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));
37                 this.factor = factor;
38                 this.levels = factor.levels();
39                 attr = new REXPList(
40                                                         new RList(
41                                                                           new REXP[] {
42                                                                                   new REXPString(this.levels), new REXPString("factor")
43                                                                           }, new String[] { "levels", "class" }));
44         }
45         
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);
51                 this.factor = factor;
52                 this.levels = factor.levels();
53         }
54
55         public boolean isFactor() { return true; }
56
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() {
60                 return factor;
61         }
62
63         public String[] asStrings() {
64                 return factor.asStrings();
65         }
66
67         public Object asNativeJavaObject() {
68                 return asStrings();
69         }
70         
71         public String toString() {
72                 return super.toString()+"["+levels.length+"]";
73         }
74 }