# Vector to Matrix. How to do it generally

I have a DataFrame, and I am creating a function that receives it, extracts a few columns and convert those columns into a matrix. I was using Matrix{T}(df) and it worked, but when the slected columns turn to be 1, it says

``````MethodError: no method matching (Matrix)(::Vector{Float64})
``````

So I read and found that there is reshape function, but I might not always have a constant number of rows or columns. I know I could define the correspondent variables but isn’t there any more elegant solution? Isn’t there any function like Matrix() that can handle the case where I have only one column?

``````julia> a = rand(3)
3-element Vector{Float64}:
0.6747880727217347
0.9481856547503953
0.9435165126351607

julia> reshape(a, :, 1)
3×1 Matrix{Float64}:
0.6747880727217347
0.9481856547503953
0.9435165126351607
``````

One way is to index the dataframe columns using vectors:

``````using DataFrames
df = DataFrame(fill.(1:4, 10), :auto)
ix = ["x1"]                 # or: ix = [1]
Matrix(df[:, ix])
ix = ["x2", "x4"]           # or: ix = [2, 4]
Matrix(df[:, ix])
``````