I have found a solution but I would like to see (and learn) different ways of achieving the result.
I also need some clarification on the solution I found.
function odds(x,car)
split(x,car)[1:2:end]
end
function evens(x,car)
split(x,car)[2:2:end]
end
transform!( df,:frqty=>(x->(zip(odds.(x,"\\"),evens.(x,"\\"))))=>[:fr,:qty])
select!(df,Not(:frqty))
ddff=vcat([DataFrame(:name=>repeat([r.name],length(r.fr)), :fr=>r.fr,:qty=>r.qty) for r in eachrow(df)]...)
Are you reading this data from some source file? If so, how are you handling that part? If it was me, I would prefer to deal with the \\ delimiter when reading the file rather than storing it like this in a DataFrame and having to sort it out afterwards. Parsing the data appropriately at the point of ingestion should also eliminate the issue you now have with your fruit and quantity columns being SubStrings.
combine(
groupby(df, :name),
:frqty => fruitinfo ∘ only => AsTable)
The problem is that you need combine if you want to do a transformation that results in more output rows than input rows, while transform needs to return the same number.
But then you want to run your function on every row, so you group by the variable name. Then you know you will receive a vector with only one element as the input in each row, so you extract that element with only (so it errors if your assumption is wrong) and then do fruitinfo on that one value. You get a list of named tuples back, and the sink argument => AsTable directly converts that into two new columns.
Thank you very much for explaining how things work.
I hadn’t read the details of the combine function description yet. I think that in this example you have produced one will find a lot of the possibilities of this function and you have saved me a mountain of time studying it.
combine(groupby(df, :name), :frqty => fruitinfo ∘ first => AsTable)
One of the questions I wanted to have about my naive solution was how to convert the string format of the qty column to numeric.
your solution solves this problem at the root.
A further lesson is that of the use of the do … end construct which I have to see because I did not know it.
Wouldn’t a “simple” map (itr, func) suffice in this case?
Now I can ask the two questions I wanted to ask.
How do you convert the qty column to integers?
And why does the transform function also put the type in front of the new column values?
Yes, this is how Vectors of SubString{String} are printed (in certain conditions). In fact, Vectors in general are printed as TypeValidForAllElements[element1, element2, ...].