TidierOrg / Tidier.jl

Meta-package for data analysis in Julia, modeled after the R tidyverse.
MIT License
524 stars 14 forks source link

Add example to docs on how to reference global variable within @mutate #107

Closed yahrMason closed 1 year ago

yahrMason commented 1 year ago

I am sure there is a way to do this, but I am having trouble figuring it out. I am trying to reference a global variable within a mutate call, but I am getting an error that says ERROR: ArgumentError: column name "x" not found in the data frame

using Tidier

df = DataFrame(a = [1,2,3], b = [4,5,6])

@chain df begin
    @mutate(c = b*10)
end

x = 10
@chain df begin
    @mutate(c = b*x)
end
kdpsingh commented 1 year ago

Try using R-style bang-bang interpolation, like this:

using Tidier

df = DataFrame(a = [1,2,3], b = [4,5,6])

@chain df begin
    @mutate(c = b*10)
end

x = 10
@chain df begin
    @mutate(c = b * !!x)
end

See here for an explanation: https://tidierorg.github.io/Tidier.jl/dev/examples/generated/UserGuide/interpolation/

Happy to answer questions. If this works, feel free to close this issue.

kdpsingh commented 1 year ago

To your point, the detailed explanation for this is in the Interpolation docs and not the @mutate() docs.

I think it's ok in Interpolation for now (just because it's a complex issue) but will consider bringing this issue up elsewhere in the docs as well to familiarize folks with this.

yahrMason commented 1 year ago

Ah! Thanks for this. Will close the issue.