@Override Val apply(Env env, Env.StackHelp stk, AST asts[]) { Val v = stk.track(asts[1].exec(env)); if (v instanceof ValRow) { ValRow vv = (ValRow) v; return vv.slice(asts[2].columns(vv._names)); } Frame fr = v.getFrame(); int[] cols = asts[2].columns(fr.names()); Frame fr2 = new Frame(); if (cols.length == 0) { // Empty inclusion list? } else if (cols[0] >= 0) { // Positive (inclusion) list if (cols[cols.length - 1] > fr.numCols()) throw new IllegalArgumentException( "Column must be an integer from 0 to " + (fr.numCols() - 1)); for (int col : cols) fr2.add(fr.names()[col], fr.vecs()[col]); } else { // Negative (exclusion) list fr2 = new Frame(fr); // All of them at first Arrays.sort(cols); // This loop depends on the values in sorted order for (int col : cols) if (0 <= -col - 1 && -col - 1 < fr.numCols()) fr2.remove(-col - 1); // Remove named column } return new ValFrame(fr2); }
/** Removes a numbered column. */ public Vec[] remove(int[] idxs) { for (int i : idxs) if (i < 0 || i > _vecs.length) throw new ArrayIndexOutOfBoundsException(); Arrays.sort(idxs); Vec[] res = new Vec[idxs.length]; Vec[] rem = new Vec[_vecs.length - idxs.length]; String[] names = new String[rem.length]; Key[] keys = new Key[rem.length]; int j = 0; int k = 0; int l = 0; for (int i = 0; i < _vecs.length; ++i) if (j < idxs.length && i == idxs[j]) { ++j; res[k++] = _vecs[i]; } else { rem[l] = _vecs[i]; names[l] = _names[i]; keys[l] = _keys[i]; ++l; } _vecs = rem; _names = names; _keys = keys; assert l == rem.length && k == idxs.length; return res; }