udibehar
(Ehud Behar)
January 11, 2024, 12:39pm
1
Suppose I have the following two datasets:
d1 = Dataset[{
<|"x" -> 195, "y" -> 2.0|>,
<|"x" -> 198, "y" -> 6.4|>,
<|"x" -> 204, "y" -> -3.0|>
}]
d2 = Dataset[{
<|"x" -> 195, "z" -> 3.9|>,
<|"x" -> 198, "z" -> 4.5|>,
<|"x" -> 204, "z" -> 1.2|>
}]
I want to multiply the y values from d1 with the z values of d2, and to create the new dataset
Dataset[{
<|"x" -> 195, "w" -> 7.8|>,
<|"x" -> 198, "w" -> 28.8|>,
<|"x" -> 204, "w" -> -3.6|>
}]
Any ideas?
eric3
(Eric Rimbey)
January 11, 2024, 4:03pm
2
Maybe something along these lines?
JoinAcross[d1, d2, "x"][All, <|"x" -> #x, "w" -> #y #z|> &]
udibehar
(Ehud Behar)
January 11, 2024, 4:56pm
3
Yes, looks good.
What if d1 and d2 both have the same names for the columns?
That is,
d1 = Dataset[{
<|"x" -> 195, "y" -> 2.0|>,
<|"x" -> 198, "y" -> 6.4|>,
<|"x" -> 204, "y" -> -3.0|>
}]
d2 = Dataset[{
<|"x" -> 195, "y" -> 3.9|>,
<|"x" -> 198, "y" -> 4.5|>,
<|"x" -> 204, "y" -> 1.2|>
}]
How do I compute y of d1 × y of d2?
In Excel, I guess you know, you can do =Sheet1!A1 * Sheet2!A1. I am looking for something similar.
I see the datasets d1 and d2 as two separate “sheets”. Is there something wrong with this point of view when moving to Wolfram?
eric3
(Eric Rimbey)
January 11, 2024, 6:21pm
4
I think you’ll need to do the disambiguation explicitly. Maybe there is some other more clever way, but I haven’t thought of it. So, something like this:
JoinAcross[
d1[All, <|"x" -> "x", "y1" -> "y"|>],
d2[All, <|"x" -> "x", "y2" -> "y"|>], "x"][All, <|"x" -> #x, "y" -> #y1*#y2|> &]