How should I apply a function to every element of some columns in place?
julia> using DataFrames
julia> df = DataFrame(Time = [3, 4, 5], TopTemp = [70, 73, 100], BottomTemp = [50, 55, 80])
3×3 DataFrame
Row │ Time TopTemp BottomTemp
│ Int64 Int64 Int64
─────┼────────────────────────────
1 │ 3 70 50
2 │ 4 73 55
3 │ 5 100 80
julia> fahrenheit_to_celsius(x) = Int(round((x - 32) * 5 / 9))
fahrenheit_to_celsius (generic function with 1 method)
This works for one column, but I'm not sure it is the best method.
julia> transform!(df, "TopTemp" => ByRow(fahrenheit_to_celsius), renamecols = false)
3×3 DataFrame
Row │ Time TopTemp BottomTemp
│ Int64 Int64 Int64
─────┼────────────────────────────
1 │ 3 21 50
2 │ 4 23 55
3 │ 5 38 80
The same method does not work to convert both columns using a regular expression with broadcasting.
julia> transform!(df, r"Temp" .=> ByRow.(fahrenheit_to_celsius), renamecols = false)
ERROR: LoadError: MethodError: no method matching fahrenheit_to_celsius(::Int64, ::Int64)