Can someone tell me how to write a Query that adds some columns, computed from several other columns in a Dataset?
A simple way:
data = Dataset[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}]
data[All, <|"f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &]
If you want to append the collumn:
data[All, <|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &]
ok, I was wrong, I was handling a List of associations like a Dataset, for a List you have to use a Query function, nor for a Dataset, as I said in my question
data1 = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>};
now appending two new computed columns to the List:
Query[All, {"a", "b"} /* <| "a" -> #a &, "b" -> #b &,
"f1" -> #a ^2 + #b &, "f2" -> N[Sin[#b + #a]] & |>]@data1
many thanks Rodrigo
Hi Pablo,
Appending columns (which @Rodrigo mentioned) can also be done with Query.
Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]@data1
(* <|"a" -> 1, "b" -> 2, "f1" -> 3, "f2" -> 0.14112|>,
<|"a" -> 3, "b" -> 4, "f1" -> 13, "f2" -> 0.656987|>} *)
I’m confused, I get an error the first time I execute this, then if I execute In[3] again there’s no error
In[1]:= data = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}
In[2]:= ds = Dataset[data]
In[3]:= Query[All, {"a", "b"} /* <| #&, "f1" -> #a ^2 + #b &, "f2" -> N[Sin[#b + #a]] & |>]@ds
however this works fine
In[1]:= data = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}
In[2]:= ds = Dataset[data]
In[3]:= Query[All, {"a", "b"} /* <| #&, "f1" -> #a ^2 + #b &, "f2" -> N[Sin[#b + #a]] & |>]@data
this is what motivated my comment above
The pure function has to be defined correctly
data = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}
ds = Dataset[data];
Query on Dataset
Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]@ds
Query on Association
Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]@data
In both cases the Query expression is identical
qFun = Query[All, {"a", "b"} /* (<|#, "f1" -> #a^2 + #b, "f2" -> N[Sin[#b + #a]]|> &)]
qFun@ds
qFun@data
Ok, it works fine, Thank you Rohit.