Friday 27 March 2020

TableSaw: Perform Arithmetic operations on a column

You can perform Arithmetic operations on every element of a column.

Example
DoubleColumn multiplyBy2 = intColumn.multiply(2);
Multiply every element of intColumn with 2 and return new DoubleColumn.

DoubleColumn addBy2 = intColumn.add(2);
Add every element of intColumn with 2 and return new DoubleColumn.

DoubleColumn subtractBy2 = intColumn.subtract(2);
Subtract every element of intColumn by 2 and return new DoubleColumn.

DoubleColumn divisionBy2 = intColumn.divide(2);
Divide every element of intColumn by 2 and return new DoubleColumn.

App.java
package com.sample.app;

import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19 };

  IntColumn intColumn = IntColumn.create("My Primes", primes);

  DoubleColumn multiplyBy2 = intColumn.multiply(2);
  DoubleColumn addBy2 = intColumn.add(2);
  DoubleColumn subtractBy2 = intColumn.subtract(2);
  DoubleColumn divisionBy2 = intColumn.divide(2);

  System.out.println(intColumn.print() + "\n");
  System.out.println(multiplyBy2.print() + "\n");
  System.out.println(addBy2.print() + "\n");
  System.out.println(subtractBy2.print() + "\n");
  System.out.println(divisionBy2.print() + "\n");

 }
}

Output

Column: My Primes
2
3
5
7
11
13
17
19


Column: My Primes * 2.0
4.0
6.0
10.0
14.0
22.0
26.0
34.0
38.0


Column: My Primes + 2.0
4.0
5.0
7.0
9.0
13.0
15.0
19.0
21.0


Column: My Primes - 2.0
0.0
1.0
3.0
5.0
9.0
11.0
15.0
17.0


Column: My Primes / 2.0
1.0
1.5
2.5
3.5
5.5
6.5
8.5
9.5






Previous                                                    Next                                                    Home

No comments:

Post a Comment