How to convert DataFrame into Array{T,2}?

Let’s say I have a dataframe that only holds number or missings.
How can I convert it to a Array{T, 2} ? Without the names / header

Thank you

using DataFrames
df = DataFrame(a=[ 1, 2, 3, 4, missing], b= [1, 2, 3, 4, 5])
df[:,:]
7 Likes

Try this one

df1=convert(Matrix,df)
2 Likes

Matrix(df) or Matrix{T}(df) if you want to specify T that is different that is automatically inferred.

10 Likes

In the given example, Matrix{T}(df) throws an error if T is not = “Any” because one of the value is missing. Matrix(df) also works fine.

You have to choose an appropriate T of course. Note for example that Matrix{Union{Real, Missing}}(df) will work fine.

4 Likes

Great thank you!

Something I learned when trying the same thing
If df has various types Int and Float64 for me, convert(Matrix, df) will give an error, but Matrix(df) works and sets all as Float64.

1 Like

convert(Matrix, df) is not supported - indepentent of the fact what columns the df stores. You need to user the constructor.

1 Like