Gu铆a de M茅todos

Ejemplos detallados para cada categor铆a de m茅todos

Filtrado y Selecci贸n

M茅todos para filtrar y seleccionar datos

filter()

const data = ToonFactory.from(`
  productos[5]{nombre,categoria,precio,stock}:
    Laptop,Electr贸nica,1200,15
    Mouse,Electr贸nica,25,50
    Teclado,Electr贸nica,75,30
    Silla,Muebles,300,10
    Escritorio,Muebles,500,5
`);

// Filter products over $100
const expensive = data.filter(p => p.precio > 100);
console.log(expensive.all());

// Chain filters
const electronics = data
  .filter(p => p.categoria === 'Electr贸nica')
  .filter(p => p.stock > 20);

select() & drop()

// Select specific columns
const names = data.select(['nombre', 'precio']);

// Drop columns
const withoutStock = data.drop(['stock']);

head(), tail(), slice()

// First 3 rows
const first = data.head(3);

// Last 2 rows
const last = data.tail(2);

// Rows 2-4
const middle = data.slice(1, 4);

Operaciones Estad铆sticas

Calcular estad铆sticas sobre tus datos

stats()

// Complete statistics
const priceStats = data.stats('precio');
console.log(priceStats);
// Output: {
//   min: 25,
//   max: 1200,
//   avg: 420,
//   sum: 2100,
//   count: 5,
//   median: 300
// }

sum(), avg(), min(), max()

// Individual statistics
const totalStock = data.sum('stock');        // 110
const avgPrice = data.avg('precio');         // 420
const minPrice = data.min('precio');         // 25
const maxPrice = data.max('precio');         // 1200

correlation() & covariance()

// Correlation between price and stock
const corr = data.correlation('precio', 'stock');
console.log(corr);  // -0.78 (negative correlation)

// Covariance
const cov = data.covariance('precio', 'stock');
console.log(cov);

variance() & stdDev()

// Variance
const variance = data.variance('precio');

// Standard deviation
const stdDev = data.stdDev('precio');
console.log(`蟽 = ${stdDev.toFixed(2)}`);

Series Temporales

Analizar datos temporales

rolling()

const ventas = ToonFactory.from(`
  ventas[7]{mes,ingresos}:
    Ene,15000
    Feb,18000
    Mar,16500
    Abr,19000
    May,21000
    Jun,20500
    Jul,22000
`);

// 3-month moving average
const ma3 = ventas.rolling('ingresos', 3, 'avg');

// 3-month rolling sum
const sum3 = ventas.rolling('ingresos', 3, 'sum');

// 3-month rolling max
const max3 = ventas.rolling('ingresos', 3, 'max');

pctChange() & diff()

// Percentage change
const pctChange = ventas.pctChange('ingresos');
console.log(pctChange.all());
// Shows % change month over month

// Absolute difference
const diff = ventas.diff('ingresos');
console.log(diff.all());
// Shows absolute difference month over month

lag() & lead()

// Lag by 1 period (previous month)
const lagged = ventas.lag('ingresos', 1);

// Lead by 1 period (next month)
const led = ventas.lead('ingresos', 1);

// Compare with previous month
const comparison = ventas
  .lag('ingresos', 1)
  .map(row => ({
    ...row,
    crecimiento: row.ingresos - (row.ingresos_lag1 || 0)
  }));

cumSum() & cumProd()

// Cumulative sum (YTD)
const ytd = ventas.cumSum('ingresos');
console.log(ytd.all());
// Shows year-to-date totals

// Cumulative product
const cumProd = ventas.cumProd('ingresos');

Transformaciones

Transformar y remodelar datos

sortBy()

// Sort by single column (descending)
const byPrice = data.sortBy(['precio'], ['desc']);

// Sort by multiple columns
const sorted = data.sortBy(
  ['categoria', 'precio'],
  ['asc', 'desc']
);

groupBy()

// Group by category and calculate stats
const byCategory = data.groupBy(['categoria'], {
  precio: ['avg', 'min', 'max'],
  stock: ['sum']
});

console.log(byCategory.all());
// Returns aggregated data by category

map() & apply()

// Transform each row
const withDiscount = data.map(row => ({
  ...row,
  precio_descuento: row.precio * 0.9
}));

// Apply function to column
const withTax = data.apply('precio', price => price * 1.21);

rank() & percentRank()

// Rank by price
const ranked = data.rank('precio', 'desc');

// Percent rank (0-1 scale)
const percentRanked = data.percentRank('precio');

console.log(ranked.all());
// Shows rank for each product

fillna() & dropna()

// Fill null values with 0
const filled = data.fillna({ stock: 0, precio: 0 });

// Drop rows with nulls
const clean = data.dropna();

// Drop rows with nulls in specific columns
const cleanPrice = data.dropna(['precio']);