Message Boards Message Boards

0
|
1020 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to multiply two columns of two different datasets?

Posted 5 months ago

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?

POSTED BY: Ehud Behar
3 Replies
Posted 5 months ago

Maybe something along these lines?

JoinAcross[d1, d2, "x"][All, <|"x" -> #x, "w" -> #y #z|> &]
POSTED BY: Eric Rimbey
Posted 5 months ago

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?

POSTED BY: Ehud Behar
Posted 5 months ago

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|> &]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract