Hi all! I’m learning Julia and these are my first steps. I have a struct that looks like this:
struct Region
elements::Array{res_tech}
demand::Matrix{Float64}
end
where res_tech is another struct that is:
struct res_tech
priority::Int
resource::Matrix{Float64}
installed_capacity::Int
name::String
end
Now, I have created a struct based on region that is
Node = Region([wind,pv],el_dem)
where wind and pv are 2 res_tech. I want to create a dataframe (let’s call it electricity) that has as column name the res_tech.name and it’s filled the resulting from a function that multiplies res_tech.resource by res_tech.installed_capacity.
I tried several things and this is the last one:
electricity = DataFrame()
for i in eachindex(Node.elements)
col_name = Node.elements[i].name
electricity.col_name = max_production(Node.elements[i].resource,Node.elements[i].installed_capacity)
end
However I get this error:
DimensionMismatch("Array could not be broadcast to match destination")
First of all, you seem to be trying to use dot-notation indexing (electricity.col_name) within a loop and using a String object (Node.elements[i].name seems to be a string based on your struct definition). This won’t work, you need to index the dataframe with a Symbol using electricity[:, Symbol(col_name)]. However it seems that the column doesn’t even exist in your DataFrame so you’ll have to create it first.
Also note that Node.elements as an array is iterable by itself, no need to use indices:
for el in Node.elements
col_name = Symbol(el.name)
electricity[!, col_name] = max_production(el.resource, el.installed_capacity)
end
Aside from what @nilshg wrote (please read the DataFrames Documentation!), your problem is that your object is a Matrix, not a Vector. Note the Array{Float64, 2} instead of Array{Float64, 1}.
You can call vec on your object to collect it into a Vector. But I think you still have the wrong number of elements. So you have two problems
Your array is a matrix, not a vector
The length of your array (the N in N x 1) is not the correct length.