Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finixh #58 by adding the vardim keyword argument to Tables.matrix, al… #66

Merged
merged 3 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,28 @@ function table(m::AbstractMatrix; header::Vector{Symbol}=[Symbol("Column$i") for
end

"""
Tables.matrix(table)
Tables.matrix(table; transpose::Bool=false)

Materialize any table source input as a `Matrix`. If the table column types are not homogenous,
they will be promoted to a common type in the materialized `Matrix`. Note that column names are
ignored in the conversion.
ignored in the conversion. By default, input table columns will be materialized as corresponding
matrix columns; passing `transpose=true` will transpose the input with input columns as matrix rows.
"""
function matrix(table)
function matrix(table; transpose::Bool=false)
cols = Tables.columns(table)
types = schema(cols).types
T = reduce(promote_type, types)
n, p = rowcount(cols), length(types)
mat = Matrix{T}(undef, n, p)
for (i, col) in enumerate(Tables.eachcolumn(cols))
copyto!(mat, n * (i - 1) + 1, col)
if !transpose
mat = Matrix{T}(undef, n, p)
for (i, col) in enumerate(Tables.eachcolumn(cols))
mat[:, i] = col
end
else
mat = Matrix{T}(undef, p, n)
for (i, col) in enumerate(Tables.eachcolumn(cols))
mat[i, :] = col
end
end
return mat
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ end
@test eltype(mat2) == Float64
@test mat2[:, 1] == nt.a
@test !Tables.istable(mat2)
mat3 = Tables.matrix(nt; transpose=true)
@test size(mat3) == (2, 3)
@test mat3[1, :] == nt.a
@test mat3[2, :] == nt.b

tbl = Tables.table(mat) |> columntable
@test keys(tbl) == (:Column1, :Column2, :Column3)
Expand Down