Can't refer to columns with spaces in names in @mutate

I want to do a simple mutate(a=b+c), but b and c have spaces in the names. Have tried various things without success.

using DataFrames
using TidierData

df=DataFrame(“The Name”=>[4,5,6],“Second Name”=>[7,8,9])
@chain df begin @mutate(New = “The Name” + “Second Name”)
end
@chain df begin @mutate(New = :“The Name” + :“Second Name”)
end
@chain df begin @mutate(New = Symbol(“The Name”) + Symbol(“Second Name”))
end

Cheers,
Geoff Russell

2 Likes

If you surround the name with backticks (as opposed to quotes), you will be able to to refer to a column with spaces in it

@chain df begin 
  @mutate(New = `The Name` + `Second Name`)
end
3×3 DataFrame
 Row │ The Name  Second Name  New   
     │ Int64     Int64        Int64 
─────┼──────────────────────────────
   1 │        4            7     11
   2 │        5            8     13
   3 │        6            9     15

however, it may be easier to use @clean_names first to avoid using backticks

@chain df begin 
    @clean_names
    @mutate(New = the_name + second_name)
end
3×3 DataFrame
 Row │ the_name  second_name  New   
     │ Int64     Int64        Int64 
─────┼──────────────────────────────
   1 │        4            7     11
   2 │        5            8     13
   3 │        6            9     15
1 Like

Wonderful.

Many thanks.

2 Likes