withRowsSampled method
A new data frame comprising sampled rows.
Example:
print(iris.withHead(3));
print(iris.withHead(3).withRowsSampled(
5, withReplacement: true, seed: 0));
.--.------------.-----------.------------.-----------.--------.
|id|sepal_length|sepal_width|petal_length|petal_width|species |
:--+------------+-----------+------------+-----------+--------:
|1 |5.1 |3.5 |1.4 |0.2 |setosa |
|2 |4.9 |3.0 |1.4 |0.2 |setosa |
|3 |4.7 |3.2 |1.3 |0.2 |setosa |
'--'------------'-----------'------------'-----------'--------'
.--.------------.-----------.------------.-----------.--------.
|id|sepal_length|sepal_width|petal_length|petal_width|species |
:--+------------+-----------+------------+-----------+--------:
|1 |5.1 |3.5 |1.4 |0.2 |setosa |
|3 |4.7 |3.2 |1.3 |0.2 |setosa |
|2 |4.9 |3.0 |1.4 |0.2 |setosa |
|2 |4.9 |3.0 |1.4 |0.2 |setosa |
|1 |5.1 |3.5 |1.4 |0.2 |setosa |
'--'------------'-----------'------------'-----------'--------'
Implementation
DataFrame withRowsSampled(
int n, {
bool withReplacement = false,
int? seed,
}) {
final rand = math.Random(seed);
if (withReplacement) {
return withRowsAtIndices(
[for (var _ = 0; _ < n; _++) rand.nextInt(rowNumber)]);
}
if (n > rowNumber) {
throw PackhorseError.badArgument("Cannot sample $n rows from $rowNumber "
"(DataFrame.withRowsSampled).");
}
return withRowsAtIndices(
([for (var i = 0; i < rowNumber; i++) i]..shuffle(rand)).sublist(n));
}