How to export data from an array

Maybe my question is a little repetitive compared to the last one I posted here.
In this model, I would like to export the data that is in i to a .CSV file.
When compiling the model the answer I have in .CSV is this:

[150.0 150.0 151.0 151.0]

The answer I want to get is:

│ Row │         │         │
│     │ Float64 │ Float64 │
├─────┼─────────┼─────────┤
│ 1   │ 150.0   │ 151.0   │
│ 2   │ 150.0   │ 151.0   │

Why can’t I export the array data in the same way that I can view the data in i?

Thanks!!!

using JuMP,CSV,DataFrames,DelimitedFiles

i = [150.0 151.0; 150.0 151.0]

#.CSV with all fields empty
open("C:\\Users\\raquel.santos\\Desktop\\testemodel.csv", "w") do io
    writedlm(io, [i])
end
readdlm("C:\\Users\\raquel.santos\\Desktop\\testemodel.csv",Float64)
arr = [150.0 150.0 151.0 151.0]

df = reshape(arr, 2,2) |> DataFrame

CSV.write("myfilename", df)

For more information on dataframes, check out the docs Getting Started · DataFrames.jl

3 Likes

Note that in the snippet above i is already defined as a 2x2 matrix, so CSV.write("testemodel.csv", DataFrame(i)) would be sufficient.

3 Likes

Thank you for your help @nilshg and @pyrex41 !!
Despite being able to export the data, I still haven’t been able to view it the way I need it.
Here’s the model I’m working on

using JuMP,CSV, DataFrames, GLPK, GLPKMathProgInterface, DelimitedFiles
Modeltest = Model(solver = GLPKSolverMIP())

a = 1:4
t = 1:4
w = 1:2

A = a
T = t
W = w

#Parameters
CR = [11, 12]
CO =[50, 51]
WA = [1, 2, 1, 2]
RHE = [150, 151]

#Variables
@variable(Modeltest,r[A,T]>=0)
@variable(Modeltest,o[A,T]>=0)
@variable(Modeltest,wr[W,T]>=0)
@variable(Modeltest,er[W,T]>=0)

@objective(Modeltest,Min,sum(CR[WA[a]]*(r[a,t]+o[a,t])+CO[WA[a]]*o[a,t] for a in A, t in T))

for w = 1:2, t= 1:4
    m = @constraint(Modeltest,sum(r[a,t] for a in A) +wr[w,t] ==RHE[w])
    #println(m)
end

print(Modeltest)
status = solve(Modeltest)
println("Objective value:  ", JuMP.getobjectivevalue(Modeltest))

println("wr = ", JuMP.getvalue(wr))

open("testemodel.csv", "w") do io
    println(io, join([getvalue(wr[W, T])]))
    #writedlm(io, join([getvalue(wr[W, T])]))
end

p = readline("testemodel.csv")
#p = readdlm("testemodel.csv")
println(p)

Perhaps I have expressed it wrongly, my question is: How to manipulate the data in an array?
espite having already used writedlm and readdlm, I can’t export the data to .CSV the way I need it.
When exporting the data to .CSV this is the answer I have:

|150.0 150.0 150.0 150.0 | 151.0 151.0 151.0 151.0|

This is the answer I would like to get, each data in a field as in a table


│ Row │ value   │ value2  │
│     │ Float64 │ Float64 │
├─────┼─────────┼─────────┤
│ 1   │ 150.0   │ 151.0   │
│ 2   │ 150.0   │ 151.0   │
│ 3   │ 150.0   │ 151.0   │
│ 4   │ 150.0   │ 151.0   │