Skip to content

howto enumerate data

Erich Seifert edited this page Mar 14, 2016 · 2 revisions

How can I add a column with row numbers to my data?

In order to add a column with row numbers you can use the class de.erichseifert.gral.data.EnumeratedData which adds a linear enumerations to an existing data source.

Let's assume you have data source containing several values:

DataTable data = new DataTable(Double.class);
data.add(42.0);
data.add(32.1);
data.add(12.3);

Resulting in the following structure:

col 0
42.0
32.1
12.3

You can wrap it with an instance of EnumeratedData to get a new column with row numbers:

DataSource dataNew = new EnumeratedData(data);

Resulting in the following structure:

col 0 col 1
0.0 42.0
1.0 32.1
2.0 12.3

So, the values of the new column start with 0.0 and in each row the value is increased by 1.0. This step width and the number it starts with can be defined when creating EnumeratedData. If you want to start with 2.0 instead of 0.0 just use the following code instead of the one above:

DataSource dataNew = new EnumeratedData(data, 2.0, 1.0);

Resulting in the following structure:

col 0 col 1
2.0 42.0
3.0 32.1
4.0 12.3

EnumeratedData can be useful in many cases like adding a column with timestamps, or creating two-dimensional data from a one-dimensional data source.