Skip to content

Commit

Permalink
fix in rep_len and factor[$jsArray]
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbogers committed Oct 29, 2018
1 parent d18c4f2 commit 1bb4bba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//import { Renhance, $class, $attr, UseMethod, $matrix, $list, $vector, $default } from './src/s3';
import { gl } from './src/factor'
import { $buildIn, $levels, $class } from './src/s3';

const f = gl(4,3)

console.log(f)
const l1 =gl(3,4, 16);
const l2 = gl(3,4, 16, ['one', 'two', 'red', 'green'])
console.log(l1)
console.log(l2[$buildIn][$levels], l2[$buildIn][$class])
27 changes: 14 additions & 13 deletions src/factor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { promoteArray, flatten, unique, multiplexer } from './helpers';
import { seq_len, repInt, isOrdered } from './public';
import { rep_len, seq_len, repInt, isOrdered } from './public';
import { FactorType } from './types';
import { getLogger } from './logger';
import { Renhance, $jsArray, $class, $fact, $levels, $buildIn, UseMethod, $ordered } from './s3';
Expand Down Expand Up @@ -30,38 +30,39 @@ _factor[$jsArray] = function factorDefault(x: FactorType[], levels: FactorType[]
}

const _levels = promoted.filter((v, i, arr) => v !== null && !arr.includes(v, i + 1));
const flatLab = (labels !== levels) ? flatten(labels).map(v => promoCode(v)) : [];
const flatLab = (labels !== levels) ? unique(flatten(labels)) : [];

if (flatLab.length && flatLab.length !== _levels.length) {
if (flatLab.length && flatLab.length < _levels.length) {
logger.errorAndThrow(Error, `invalid labels; length ${labels.length} should be 1 or ${_levels.length}`)
}

// optional reformatting of data
//
if (flatLab.length) {
//construct mapper
const mapper = multiplexer(<any>flatLab, <any>_levels)
((lab, lev) => [lev, lab])
const mapper1 = multiplexer(<any>flatLab, <any>_levels)
((lab, lev) => ({ lv:lev, la: lab}))
const mapper2 = mapper1
.reduce((coll: { [index: string]: any }, v) => {
coll[v[0]] = v[1];
coll[v.lv] = v.la;
return coll;
}, {} as { [index: string]: any });

for (let i = 0; i < promoted.length; i++) {
const pi = mapper[<any>promoted[i]]
const pi = mapper2[<any>promoted[i]]
promoted[i] = pi;
}
}

const obj = Renhance(promoted, $buildIn);
obj[$buildIn][$levels] = levels
obj[$buildIn][$levels] = flatLab.length ? flatLab : _levels
obj[$buildIn][$class] = ordered ? [$ordered, $fact] : [$fact]

return obj;
}

export function gl(n: number, k: number, length: number = n * k, labels = seq_len(n), ordered = false) {
const data = seq_len(repInt(seq_len(n), repInt(k, n)), length)
export function gl(n: number, k: number, length: number = n * k, labels:FactorType[] = seq_len(n), ordered = false) {
const data = rep_len(repInt(seq_len(n), repInt(k, n)), length)
const fac = _factor(data, undefined, labels, undefined, ordered)
return fac;
}
Expand All @@ -70,7 +71,7 @@ export function gl(n: number, k: number, length: number = n * k, labels = seq_le
### factors
### levels
```R
base::gl #Generate factors by specifying the pattern of their levels._
xx base::gl #Generate factors by specifying the pattern of their levels._
base::Ops.ordered
base::addNA
base::[<-.factor
Expand All @@ -91,9 +92,9 @@ base::is.na<-.factor
base::ordered
base::all.equal.factor
base::levels
base::is.ordered
xx base::is.ordered
base::levels.default
base::factor
xx base::factor
base::nlevels
base::Summary.ordered
base::as.ordered
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function duplicates(input: any[]): any[] {
}

export function unique(input: any[]): any[] {
return input.filter((f, i, arr) => indexOf(f, i + 1, arr) !== -1);
return input.filter((f, i, arr) => indexOf(f, i + 1, arr) === -1);
}

export function promote(...args: FactorType[]) {
Expand Down
4 changes: 2 additions & 2 deletions src/public.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { UseMethod, Rclass, /* $default, */ $ordered, $list, $df, $int, $string, $double, $logical, $jsArray, isR } from "./s3";
import { UseMethod, Rclass, /* $default, */ $ordered, $list, $df, $int, $string, $double, $logical, $jsArray } from "./s3";
import { multiplexer } from './helpers';

function seq_len(length: number, offset = 1) {
Expand Down Expand Up @@ -43,7 +43,7 @@ rep_len[$jsArray] = function (x: any, length: number) {
}
rc = Array.from({ length });
for (let i = 0; i < rc.length; i++) {
rc[i] = x[i % length]
rc[i] = x[i % x.length]
}
return rc;
}
Expand Down

0 comments on commit 1bb4bba

Please sign in to comment.