Skip to main content
Version: Java (Groovy)

How to create new columns in a table

This guide will show you how create new columns in your tables.

selection methods, such as select, view, update, updateView, and lazyUpdate, and formulas are used to create new columns. The selection method determines which columns will be in the output table and how the values are computed. The formulas are the recipes for computing the cell values.

In the following examples, we use a table of student test results. Using update , we create a new column (Total) containing the sum of the math, science, and art scores for each student. Notice that update includes the columns from the source table in the output table.

scores = newTable(
stringCol("Name", "James", "Lauren", "Zoey"),
intCol("Math", 95, 72, 100),
intCol("Science", 100, 78, 98),
intCol("Art", 90, 92, 96)
)

total = scores.update("Total = Math + Science + Art")

Now we make the example a little more complicated by adding a column of average test scores.

average = scores.update("Average = (Math + Science + Art) / 3 ")

For the next example, we have the students' test results in various subjects and the class averages. We want to see which students scored higher than the class average. We can use the select method to create a table containing the Name and Subject columns from the source table, plus a new column indicating if the score is above average.

classAverage = newTable(
stringCol("Name", "James", "James", "James", "Lauren", "Lauren", "Lauren", "Zoey", "Zoey", "Zoey"),
stringCol("Subject", "Math", "Science", "Art", "Math", "Science", "Art", "Math", "Science", "Art"),
intCol("Number", 95, 100, 90, 72, 78, 92, 100, 98, 96),
intCol("ClassAverage", 90, 87, 65, 93, 88, 98, 80, 77, 95),
)

aboveAverage = classAverage.select("Name", "Subject", "AboveAverage = Number > ClassAverage")