Ejemplo n.º 1
0
    public static RString substring(RString text, RDouble first, RDouble last, ASTNode ast) {
        int textSize = text.size();
        int firstSize = first.size();
        int lastSize = last.size();
        int textIndex = 0;
        int firstIndex = 0;
        int lastIndex = 0;
        if (textSize == 0) { return RString.EMPTY; }
        if (firstSize == 0) { throw RError.getInvalidArgument(ast, "first"); }// not exactly R-warning        
        if (lastSize == 0) { throw RError.getInvalidArgument(ast, "last"); } // not exactly R-warning        

        int n = Math.max(textSize, Math.max(firstSize, lastSize));

        String[] content = new String[n];
        for (int i = 0; i < n; i++) {
            double nfirst = first.getDouble(firstIndex++);
            if (firstIndex == firstSize) {
                firstIndex = 0;
            }
            double nlast = last.getDouble(lastIndex++);
            if (lastIndex == lastSize) {
                lastIndex = 0;
            }
            String str = text.getString(textIndex++);
            if (textIndex == textSize) {
                textIndex = 0;
            }
            if (!RDouble.RDoubleUtils.isNAorNaN(nfirst) && !RDouble.RDoubleUtils.isNAorNaN(nlast) && str != RString.NA) {
                int stp = (int) nlast;
                int len = str.length();
                if (stp > len) {
                    stp = len;
                }
                content[i] = str.substring(((int) nfirst) - 1, stp);
            } else {
                content[i] = RString.NA;
            }
        }
        return RString.RStringFactory.getFor(content);
    }
Ejemplo n.º 2
0
 public static boolean columnsNAMap(double[] x, int nrow, int ncol, boolean[] hasNA) {
     boolean anyNA = false;
     for (int j = 0; j < ncol; j++) {
         int offset = j * nrow;
         for (int i = 0; i < nrow; i++) {
             if (RDouble.RDoubleUtils.isNAorNaN(x[offset + i])) {
                 hasNA[j] = true;
                 anyNA = true;
                 break;
             }
         }
     }
     return anyNA;
 }
Ejemplo n.º 3
0
 public static double[] columnMeansNoNA(double[] x, int nrow, int ncol) {
     double[] res = new double[ncol];
     for (int j = 0; j < ncol; j++) {
         int offset = j * nrow;
         double sum = 0;
         for (int k = 0; k < nrow; k++) {
             sum += x[offset + k];
         }
         double tmp = sum / nrow;
         if (RDouble.RDoubleUtils.isFinite(tmp)) {
             sum = 0;
             for (int k = 0; k < nrow; k++) {
                 sum += x[offset + k] - tmp;
             }
             tmp += sum / nrow;
         }
         res[j] = tmp;
     }
     return res;
 }
Ejemplo n.º 4
0
 public static double[] columnMeans(double[] x, int nrow, int ncol, boolean[] hasNA) {
     double[] res = new double[ncol];
     for (int j = 0; j < ncol; j++) {
         if (hasNA[j]) {
             res[j] = RDouble.NA;
             continue;
         }
         int offset = j * nrow;
         double sum = 0;
         for (int k = 0; k < nrow; k++) {
             sum += x[offset + k];
         }
         double tmp = sum / nrow;
         if (RDouble.RDoubleUtils.isFinite(tmp)) {
             sum = 0;
             for (int k = 0; k < nrow; k++) {
                 sum += x[offset + k] - tmp;
             }
             tmp += sum / nrow;
         }
         res[j] = tmp;
     }
     return res;
 }