Using println to insert string into table

Hello!
Can someone explain to me why the command in the second loop produces an error? My hunch is that somehow quotes “” are missing in the println statement.

The MWE is here:

results_all_models = DataFrame( Model = "Single Cycle",
                                ParameterName="Log-likelihood",
                                Value = 1400);

namevec = ["p 1", "p 2"];
values = [1, 2]

for ins in zip(namevec,values)
    push!(results_all_models,["Single Cycle", ins[1] ,ins[2]]);
end

for ins in zip(namevec,values)
    push!(results_all_models,["Single Cycle", println(ins[1],"_tstat"), ins[2]]);
end

and the error message is here:

p 1_tstat
p 2_tstat
p 1_tstat
┌ Error: Error adding value to column :ParameterName. Maybe you forgot passing `promote=true`?
└ @ DataFrames /Users/jerome/.julia/packages/DataFrames/58MUJ/src/dataframe/insertion.jl:697
MethodError: Cannot `convert` an object of type Nothing to an object of type String

println doesn’t return a string, it prints directly to stdout and returns nothing. Use string(ins[1], "_tstat") or printf from the Printf stdlib instead.

1 Like

*@sprintf

1 Like

Right, that’s a typo :laughing: Thank you!