donPablo
donPablo

Reputation: 1959

Julia -- no method matching +(::Float64, ::Vector{Float64})

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

Upvotes: 1

Views: 1503

Answers (1)

Karl Roldan
Karl Roldan

Reputation: 387

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])

Upvotes: 3

Related Questions