# Handling a "Mixed Type" Matrix (String, Float64 and Int64)

**URL:** <https://discourse.julialang.org/t/handling-a-mixed-type-matrix-string-float64-and-int64/64910>\
**Category:** General Usage\
**Created:** [July 19, 2021, 5:53am UTC](https://discourse.julialang.org/t/handling-a-mixed-type-matrix-string-float64-and-int64/64910 "2021-07-19T05:53:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [July 19, 2021, 5:53am UTC](https://discourse.julialang.org/t/handling-a-mixed-type-matrix-string-float64-and-int64/64910/1 "2021-07-19T05:53:47Z")

</div>

I am trying to copy a string vector into a numerical matrix (2D) column and having some difficulty. I realise Julia one needs to take note on how to define the matrix type. Please see my simple test example below. The code as written works, as I am only dealing with integers. However, when I try to add the string vector (Vector\_Month\_Names) to myMatrix I get errors. Can anyone see what I am doing wrong or am I trying to do the impossible?

Thanks,

Peter

```julia
using CSV
using DataFrames
using DelimitedFiles

myMatrix =zeros(12,2)
#myMatrix = Array{Any,2}
Vector_Month_Names = Array{String,1}
Vector_Month_Names = ["Jan","Feb","Mar","Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

function foo()
    j=12

    for i in 1:j
        a= i 
        b=2*i
        c = Vector_Month_Names[i]
        myMatrix[i,:] = vcat(a,b)
        #myMatrix[i,:] = vcat(a,b,c)
    end

    df=DataFrame(myMatrix)
    CSV.write(joinpath(pwd(),"Test_Ouput.csv"), df, delim=',',decimal='.', newline="\r\n") # Writes to current directory
    println(myMatrix[12,2], Vector_Month_Names[3])

end

foo()

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 19, 2021, 6:22am UTC](https://discourse.julialang.org/t/handling-a-mixed-type-matrix-string-float64-and-int64/64910/2 "2021-07-19T06:22:59Z")

</div>

You could consider doing this:

```julia
julia> T = Union{String,Int}
Union{Int64, String}

julia> myMatrix = Matrix{T}(undef,12,3)
12×3 Matrix{Union{Int64, String}}:
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef

julia> for i in 1:j
           a= i 
           b=2*i
           c = Vector_Month_Names[i]
           myMatrix[i,:] = vcat(a,b,c)
       end

julia> myMatrix
12×3 Matrix{Union{Int64, String}}:
  1 2 "Jan"
  2 4 "Feb"
  3 6 "Mar"
  4 8 "Apr"
  5 10 "May"
  6 12 "Jun"
  7 14 "Jul"
  8 16 "Aug"
  9 18 "Sep"
 10 20 "Oct"
 11 22 "Nov"
 12 24 "Dec"

```

Otherwise, I think DataFrames.jl is what you might want instead of a matrix.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [July 19, 2021, 7:18am UTC](https://discourse.julialang.org/t/handling-a-mixed-type-matrix-string-float64-and-int64/64910/3 "2021-07-19T07:18:20Z")

</div>

Perfect. Many thanks Peter
