head
Builtin index · Collections & strings
result = head(s, n)
Input: s, n · Output: result
What it is
s is a Str, List, Vec, or Table; n (number, default 5 when used on a table).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = head(s, n) | s Str, List, Vec, or Tables is a Str, List, Vec, or Table.n numbern (number, default 5 when used on a table). | result — | Returns the first (head) or last (tail) n elements/rows, same type as s. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
t = read_csv(path, headers = true)
print("rows: {nrow(t)}")
print(head(t, 2))
from agent-guide
write_csv(table(
temp = [21.5, 22.1, 35.0, 21.8, 36.2, 22.0, 34.5, 21.6, 35.8, 22.3],
humidity = [45.2, 46.0, 20.1, 45.5, 19.5, 45.8, 21.0, 45.1, 20.4, 46.2],
pressure = [1012.1, 1011.8, 1009.5, 1012.0, 1008.9, 1012.3, 1009.8, 1012.2, 1009.1, 1011.9],
label = ["ok", "ok", "warn", "ok", "warn", "ok", "warn", "ok", "warn", "ok"]
), "sensors.csv")
df = read_csv("sensors.csv")
print("rows = {nrow(df)}, cols = {ncol(df)}")
print(head(df, 3))
numeric = drop(df, "label")
c = corrmat(numeric)
print(c.matrix)
c.plot()
savefig("quickstart_corrmat.svg")
scaled_temp = standardize(df.temp)
print("standardized temp[0] = {scaled_temp[0]:.4f}")
print("mean(standardized) = {mean(scaled_temp):.4f}, std(standardized) = {std(scaled_temp):.4f}")
output
rows = 10, cols = 4 temp humidity pressure label ---- -------- -------- ----- 21.5 45.2 1012.1 ok 22.1 46 1011.8 ok 35 20.1 1009.5 warn [1, -0.997357, -0.991317; -0.997357, 1, 0.983511; -0.991317, 0.983511, 1] · savefig quickstart_corrmat.svg note: fonts render using whatever is installed on the viewer's machine (none are embedded) -- pass embed_fonts=true (fontfamily preset only) or export .tikz for guaranteed appearance standardized temp[0] = -0.8275 mean(standardized) = -0.0000, std(standardized) = 1.0000
from quickstart
How it works
head/tail of the first thing you reach for them with. A table was the only one they took, so head(s, 2) on a string -- the obvious spelling -- was an error naming tables.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.
See also
tail — documented in the same entry.