You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julia>@chain df begin@mutate(across((a, b), x ->if_else.(x .<5, "OK", "NOK")))
end
3×5 DataFrame
Row │ a b c a_function b_function
│ Int64 Int64 Int64 String String
─────┼─────────────────────────────────────────────
1 │ 5 2 10 NOK OK
2 │ 2 4 2 OK OK
3 │ 6 6 1 NOK NOK
across() is able to accept anonymous functions like the one shown here. The one caveat with across() is that it operates on entire columns and doesn't auto-vectorize the contents of functions. So when you define the anonymous function, you have to vectorize if_else() as if_else.() and x < 5 as x .< 5.
3×5 DataFrame
Row │ a b c a_okay b_okay
│ Int64 Int64 Int64 String String
─────┼─────────────────────────────────────
1 │ 5 2 10 NOK OK
2 │ 2 4 2 OK OK
3 │ 6 6 1 NOK NOK
We can do this in R:
library(dplyr)
df <- data.frame(
a = c(5, 2, 6),
b = c(2, 4, 6),
c = c(10, 2, 1)
)
df %>%
mutate(across((a:c), ~ if_else(. < 5, "OK", "NOK"), .names = "new{col}"))
In Julia, I tried that:
using Tidier
df = DataFrame(a = [5, 2, 6], b = [2, 4, 6], c = [10, 2, 1])
@chain df begin
@Mutate(across((a, b), if_else(.<5, "OK", "NOK") ))
end
But:
ERROR: ParseError:
Error @ REPL[102]:2:31
@chain df begin
@Mutate(across((a:c), if_else(.<5, "OK", "NOK") ))
└┘ ── not a unary operator
Stacktrace:
[1] top-level scope
@ none:1
You can use the “.” operator in Tidier and apply .names (as done in R) to avoid writing them out one by one:
@chain df begin
@Mutate(
newa = ifelse.(a .< 5, "OK", "NOK"),
newb = ifelse.(b .< 5, "OK", "NOK"),
newc = ifelse.(c .< 5, "OK", "NOK")
)
end
The text was updated successfully, but these errors were encountered: