1

I started out without dot-notation, and after adding it I am still getting an error. What will make this work? Is there a book or web-url that descripes the syntax rules for solving this?

I am running this in Julia Version 1.9.3 (2023-08-24) using VSCode Version: 1.83.0 on Windows_NT x64 10.0.19045 tkx, Paul

# Failing.jl -- example
using DataFrames
using Random

Random.seed!(1234)

start_year = 2015
end_year   = 2020
n_steps = length(start_year:end_year)

thisResult =  DataFrame(years = [start_year:1:end_year;],
                        net_output = rand(n_steps))

discount2pct = DataFrame(pct = fill(0.02,n_steps)[:,1])  
colName = "Disc_2pct"

thisResult[!, "$colName"] .=   thisResult[:, :net_output] ./ ((1.0 + discount2pct[:, :pct]) .^ (2015 - thisResult[:,years]))

ERROR: LoadError: MethodError: no method matching +(::Float64, ::Vector{Float64}) For element-wise addition, use broadcasting with dot syntax: scalar .+ array Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) @ Base operators.jl:578 +(::T, ::T) where T<:Union{Float16, Float32, Float64} @ Base float.jl:408
+(::Union{Float16, Float32, Float64}, ::BigFloat) @ Base mpfr.jl:423 ... Stacktrace: [1] top-level scope @ c:\Failing.jl:13

3
  • 1
    it seems like you're wanting something like this: thisResult[:, :net_output] ./ ((1.0 .+ discount2pct[:, :pct]) .^ (2015 .- thisResult[:, :years])) Commented Oct 15, 2023 at 2:29
  • 1
    though without a desired output it's hard to say Commented Oct 15, 2023 at 2:29
  • @Mark this code has been abstracted from the real problem to isolate the failure. When I put your solution into the real code it gives good data. Commented Oct 15, 2023 at 2:50

1 Answer 1

3

Take note that discount2pcd[:, :pct] and thisResult[:,years] are both Vector{Float64}. You cannot simply add a scalar to vectors in Julia so you also need to use the broadcast dot notation.

(1.0 .+ discount2pct[:, :pct]) .^ (2015 .- thisResult[:,years])
Sign up to request clarification or add additional context in comments.

2 Comments

MANY thanks for the quick solution. IT WORKS in the real code with real data. I had overlooked the two additional operators, and the colon in front of :years
ERROR: UndefVarError: years not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.