Java Code Examples for tech.tablesaw.api.IntColumn#set()

The following examples show how to use tech.tablesaw.api.IntColumn#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributionVisualizations.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table property = Table.read().csv("../data/sacramento_real_estate_transactions.csv");

    IntColumn sqft = property.intColumn("sq__ft");
    IntColumn price = property.intColumn("price");

    sqft.set(sqft.isEqualTo(0), IntColumnType.missingValueIndicator());
    price.set(price.isEqualTo(0), IntColumnType.missingValueIndicator());

    Plot.show(Histogram.create("Distribution of prices", property.numberColumn("price")));

    Layout layout = Layout.builder().title("Distribution of property sizes").build();
    HistogramTrace trace =
        HistogramTrace.builder(property.numberColumn("sq__ft"))
            .marker(Marker.builder().color("#B10DC9").opacity(.70).build())
            .build();
    Plot.show(new Figure(layout, trace));

    Plot.show(Histogram2D.create("Distribution of price and size", property, "price", "sq__ft"));

    Plot.show(BoxPlot.create("Prices by property type", property, "type", "price"));
  }
 
Example 2
Source File: Summarizer.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private IntColumn assignToGroupsByStep(int step) {
  IntColumn groupColumn = IntColumn.create(GROUP_COL_TEMP_NAME, temp.rowCount());
  temp.addColumns(groupColumn);

  int groupId = 1;
  int withinGroupCount = 0;
  Row row = new Row(temp);

  while (row.hasNext()) {
    row.next();
    if (withinGroupCount < step) {
      withinGroupCount++;
      groupColumn.set(row.getRowNumber(), groupId);
    } else {
      groupId++;
      groupColumn.set(row.getRowNumber(), groupId);
      withinGroupCount = 1;
    }
  }
  int lastGroupSize = temp.where(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId)).rowCount();
  if (lastGroupSize < step) {
    temp = temp.dropWhere(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId));
  }
  temp.addColumns(IntColumn.indexColumn("index", temp.rowCount(), 1));
  return groupColumn;
}
 
Example 3
Source File: DistributionVisualizations.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table property = Table.read().csv("../data/sacramento_real_estate_transactions.csv");

    IntColumn sqft = property.intColumn("sq__ft");
    IntColumn price = property.intColumn("price");

    sqft.set(sqft.isEqualTo(0), IntColumnType.missingValueIndicator());
    price.set(price.isEqualTo(0), IntColumnType.missingValueIndicator());

    Plot.show(Histogram.create("Distribution of prices", property.numberColumn("price")));

    Layout layout = Layout.builder().title("Distribution of property sizes").build();
    HistogramTrace trace =
        HistogramTrace.builder(property.numberColumn("sq__ft"))
            .marker(Marker.builder().color("#B10DC9").opacity(.70).build())
            .build();
    Plot.show(new Figure(layout, trace));

    Plot.show(Histogram2D.create("Distribution of price and size", property, "price", "sq__ft"));

    Plot.show(BoxPlot.create("Prices by property type", property, "type", "price"));
  }
 
Example 4
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn minute() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "minute" + "]", size());
  for (int r = 0; r < size(); r++) {
    if (!isMissing(r)) {
      long c1 = getLongInternal(r);
      newColumn.set(r, getMinute(c1));
    }
  }
  return newColumn;
}
 
Example 5
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfYear() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of year", this.size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, (short) getDayOfYear(c1));
    }
  }
  return newColumn;
}
 
Example 6
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfWeekValue() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of week value", this.size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, (short) getDayOfWeek(c1).getValue());
    }
  }
  return newColumn;
}
 
Example 7
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a day
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default IntColumn timeWindow(ChronoUnit unit, int n, LocalDate start) {
  String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
  int packedStartDate = PackedLocalDate.pack(start);
  IntColumn numberColumn = IntColumn.create(newColumnName, size());
  for (int i = 0; i < size(); i++) {
    int packedDate = getIntInternal(i);
    int result;
    switch (unit) {
      case DAYS:
        result = PackedLocalDate.daysUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case WEEKS:
        result = PackedLocalDate.weeksUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case MONTHS:
        result = PackedLocalDate.monthsUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case YEARS:
        result = PackedLocalDate.yearsUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      default:
        throw new UnsupportedTemporalTypeException(
            "The ChronoUnit " + unit + " is not supported for timeWindows on dates");
    }
  }
  numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
  return numberColumn;
}
 
Example 8
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfWeekValue() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of week", this.size());
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.setMissing(r);
    } else {
      newColumn.set(r, (short) PackedLocalDate.getDayOfWeek(c1).getValue());
    }
  }
  return newColumn;
}
 
Example 9
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfMonth() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of month", size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, getDayOfMonth(c1));
    }
  }
  return newColumn;
}
 
Example 10
Source File: TornadoVisualizations.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
    out(tornadoes.structure());
    tornadoes.setName("tornadoes");

    // filter out a bad data point
    tornadoes = tornadoes.where(tornadoes.numberColumn("Start Lat").isGreaterThan(20f));

    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Plot.show(
        PiePlot.create("Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Table fatalities2 = tornadoes.summarize("fatalities", sum).by("state");

    Plot.show(
        ParetoPlot.createVertical(
            "Total Tornado Fatalities by State", fatalities2, "state", "sum [fatalities]"));

    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");
    Plot.show(
        HorizontalBarPlot.create(
            "Tornado Injuries by Scale", injuries1, "scale", "mean [injuries]"));
    out(injuries1);
  }
 
Example 11
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfMonth() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of month", size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, getDayOfMonth(c1));
    }
  }
  return newColumn;
}
 
Example 12
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfYear() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of year", this.size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, (short) getDayOfYear(c1));
    }
  }
  return newColumn;
}
 
Example 13
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfWeekValue() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of week value", this.size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, (short) getDayOfWeek(c1).getValue());
    }
  }
  return newColumn;
}
 
Example 14
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a day
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default IntColumn timeWindow(ChronoUnit unit, int n, LocalDate start) {
  String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
  int packedStartDate = PackedLocalDate.pack(start);
  IntColumn numberColumn = IntColumn.create(newColumnName, size());
  for (int i = 0; i < size(); i++) {
    int packedDate = getIntInternal(i);
    int result;
    switch (unit) {
      case DAYS:
        result = PackedLocalDate.daysUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case WEEKS:
        result = PackedLocalDate.weeksUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case MONTHS:
        result = PackedLocalDate.monthsUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case YEARS:
        result = PackedLocalDate.yearsUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      default:
        throw new UnsupportedTemporalTypeException(
            "The ChronoUnit " + unit + " is not supported for timeWindows on dates");
    }
  }
  numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
  return numberColumn;
}
 
Example 15
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfWeekValue() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of week", this.size());
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.setMissing(r);
    } else {
      newColumn.set(r, (short) PackedLocalDate.getDayOfWeek(c1).getValue());
    }
  }
  return newColumn;
}
 
Example 16
Source File: TornadoVisualizations.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
    out(tornadoes.structure());
    tornadoes.setName("tornadoes");

    // filter out a bad data point
    tornadoes = tornadoes.where(tornadoes.numberColumn("Start Lat").isGreaterThan(20f));

    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Plot.show(
        PiePlot.create("Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Table fatalities2 = tornadoes.summarize("fatalities", sum).by("state");

    Plot.show(
        ParetoPlot.createVertical(
            "Total Tornado Fatalities by State", fatalities2, "state", "sum [fatalities]"));

    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");
    Plot.show(
        HorizontalBarPlot.create(
            "Tornado Injuries by Scale", injuries1, "scale", "mean [injuries]"));
    out(injuries1);
  }
 
Example 17
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn minute() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "minute" + "]", size());
  for (int r = 0; r < size(); r++) {
    if (!isMissing(r)) {
      long c1 = getLongInternal(r);
      newColumn.set(r, getMinute(c1));
    }
  }
  return newColumn;
}
 
Example 18
Source File: BarExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table table = Table.read().csv("../data/tornadoes_1950-2014.csv");
  NumericColumn<?> logNInjuries = table.numberColumn("injuries").add(1).logN();
  logNInjuries.setName("log injuries");
  table.addColumns(logNInjuries);
  IntColumn scale = table.intColumn("scale");
  scale.set(scale.isLessThan(0), IntColumnType.missingValueIndicator());

  Table summaryTable = table.summarize("fatalities", "log injuries", sum).by("Scale");

  Plot.show(
      HorizontalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.STACK,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Plot.show(
      VerticalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.GROUP,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Layout layout =
      Layout.builder()
          .title("Tornado Impact")
          .barMode(Layout.BarMode.GROUP)
          .showLegend(true)
          .build();

  String[] numberColNames = {"Sum [Fatalities]", "Sum [log injuries]"};
  String[] colors = {"#85144b", "#FF4136"};

  Trace[] traces = new Trace[2];
  for (int i = 0; i < 2; i++) {
    String name = numberColNames[i];
    BarTrace trace =
        BarTrace.builder(summaryTable.categoricalColumn("scale"), summaryTable.numberColumn(name))
            .orientation(BarTrace.Orientation.VERTICAL)
            .marker(Marker.builder().color(colors[i]).build())
            .showLegend(true)
            .name(name)
            .build();
    traces[i] = trace;
  }
  Plot.show(new Figure(layout, traces));
}
 
Example 19
Source File: BarExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table table = Table.read().csv("../data/tornadoes_1950-2014.csv");
  NumericColumn<?> logNInjuries = table.numberColumn("injuries").add(1).logN();
  logNInjuries.setName("log injuries");
  table.addColumns(logNInjuries);
  IntColumn scale = table.intColumn("scale");
  scale.set(scale.isLessThan(0), IntColumnType.missingValueIndicator());

  Table summaryTable = table.summarize("fatalities", "log injuries", sum).by("Scale");

  Plot.show(
      HorizontalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.STACK,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Plot.show(
      VerticalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.GROUP,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Layout layout =
      Layout.builder()
          .title("Tornado Impact")
          .barMode(Layout.BarMode.GROUP)
          .showLegend(true)
          .build();

  String[] numberColNames = {"Sum [Fatalities]", "Sum [log injuries]"};
  String[] colors = {"#85144b", "#FF4136"};

  Trace[] traces = new Trace[2];
  for (int i = 0; i < 2; i++) {
    String name = numberColNames[i];
    BarTrace trace =
        BarTrace.builder(summaryTable.categoricalColumn("scale"), summaryTable.numberColumn(name))
            .orientation(BarTrace.Orientation.VERTICAL)
            .marker(Marker.builder().color(colors[i]).build())
            .showLegend(true)
            .name(name)
            .build();
    traces[i] = trace;
  }
  Plot.show(new Figure(layout, traces));
}
 
Example 20
Source File: BarPieAndParetoExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    // ***************** Setup *************************
    // load the data into a table
    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");

    // Get the scale column and replace any values of -9 with the column's missing value indicator
    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    // ***************** Plotting **********************

    // BAR PLOTS

    // Sum the number of fatalities from each tornado, grouping by scale
    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    // Plot
    Plot.show(
        HorizontalBarPlot.create(
            "fatalities by scale", // plot title
            fatalities1, // table
            "scale", // grouping column name
            "sum [fatalities]")); // numeric column name

    // Plot the mean injuries rather than a sum.
    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Average number of tornado injuries by scale", injuries1, "scale", "mean [injuries]"));

    // PIE PLOT
    Plot.show(PiePlot.create("fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    // PARETO PLOT
    Table t2 = tornadoes.summarize("fatalities", sum).by("State");

    t2 = t2.sortDescendingOn(t2.column(1).name());
    Layout layout = Layout.builder().title("Tornado Fatalities by State").build();
    BarTrace trace = BarTrace.builder(t2.categoricalColumn(0), t2.numberColumn(1)).build();
    Plot.show(new Figure(layout, trace));
  }