Sunday 29 March 2020

TableSaw: Selection.selectNRowsAtRandom : Select n random rows

‘Selection.selectNRowsAtRandom’ method is used to select ‘n’ random rows.

Example
IntColumn filteredElements = intColumn.where(Selection.selectNRowsAtRandom(4, intColumn.size()));

Above statement select 4 random rows.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.selection.Selection;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 2, 4, 103, 24, 53, 23, 12, 11, 56, 6 };

  IntColumn intColumn = IntColumn.create("My Numbers", numbers);

  IntColumn filteredElements = intColumn.where(Selection.selectNRowsAtRandom(4, intColumn.size()));

  System.out.println(intColumn.print() + "\n");

  filteredElements.setName("Filtered Elements");
  System.out.println(filteredElements.print());
 }
}

Output
Column: My Numbers
2
4
103
24
53
23
12
11
56
6


Column: Filtered Elements
4
24
12
6




Previous                                                    Next                                                    Home

No comments:

Post a Comment