How do you add/subtract/muiltply/divide a column in my dataframe by a scalar?
@> begin
dat
@transform(new_col=:x ./365)
throws an error:
MethodError: no method matching /(::Symbol, ::Int64)
thoughts? just want to convert days to years. Had Similar trouble with +, -, etc.
jbrea
November 12, 2019, 4:55pm
2
Do you want to do something like this:
julia> using DataFrames
julia> df = DataFrame(a = rand(3), b = rand(3))
3×2 DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 0.254205 │ 0.843809 │
│ 2 │ 0.454499 │ 0.106612 │
│ 3 │ 0.646103 │ 0.286037 │
julia> df.a ./= 365
3-element Array{Float64,1}:
0.0006964531670979474
0.0012452038656085495
0.0017701452273107896
julia> df
3×2 DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼─────────────┼──────────┤
│ 1 │ 0.000696453 │ 0.843809 │
│ 2 │ 0.0012452 │ 0.106612 │
│ 3 │ 0.00177015 │ 0.286037 │
or
julia> using DataFramesMeta
julia> df = @transform df c = :b ./ 365
3×3 DataFrame
│ Row │ a │ b │ c │
│ │ Float64 │ Float64 │ Float64 │
├─────┼─────────────┼──────────┼─────────────┤
│ 1 │ 0.000696453 │ 0.843809 │ 0.00231181 │
│ 2 │ 0.0012452 │ 0.106612 │ 0.000292088 │
│ 3 │ 0.00177015 │ 0.286037 │ 0.000783664 │
The latter is what I was trying to do, but looks like you managed to do it. Maybe it has something to do with my versions?
Julia v1.0.3 and DataFramesMeta v0.6?
jbrea
November 12, 2019, 7:59pm
4
I am on Julia v1.2.0 and DataFramesMeta v0.5.0.
Does the example I posted not work with your version?
I couldn’t try your example, since I don’t have dat
. But note that in my example the dataframe df
is the first argument of the marcro @transform
.