# Row Keys in Datasets: JoinAcross row keys; Demoting or deleting row keys

I want to combine two datasets that share row keys but have different column keys. Visually, this looks like a JoinAcross on the row keys to me, but the column of row keys doesn't appear to be named. Can I perform this combination as a JoinAcross, or does it require a different function?

Also, the row keys are sometimes cumbersome--is there a way to demote them to normal rows?

Example of desired dataset combination

I have two datasets:

dsA = {<|"keyA" -> <|"key1" -> "a", "key2" -> "b"|>,
"keyB" -> <|"key1" -> "c", "key2" -> "d"|>|>} // Dataset;
dsB= {<|"keyA" -> <|"key3" -> "a", "key4" -> "b"|>,
"keyB" -> <|"key3" -> "d", "key4" -> "f"|>|>} // Dataset;


I want the join these such that I get a third dataset:

dsNew ={<|"keyA" -> <|"key1" -> "a", "key2" -> "b", "key3" -> "a",
"key4" -> "b"|>,
"keyB" -> <|"key1" -> "c", "key2" -> "d", "key3" -> "d",
"key4" -> "f"|>|>} // Dataset


I have tried JoinAcross[ dsA, dsB, Keys[dsA]], but this produces unclear errors.

Example of desired Key demotion

I have dsA again. I want to "demote" its row keys, such that I get:

{<|"RowKeys" -> "keyA", "key1" -> "a", "key2" -> "b"|>,
<|"RowKeys" -> "keyB", "key1" -> "c", "key2" -> "d"|>} // Dataset


Join at level 3:

dsC = Join[dsA, dsB, 3]


For the second part of the question:

Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]] @ dsA


Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]] @ dsC


• The Apply method works for a two row dataset, but appears to fail for datasets with more than two rows. For example, if I say dsC=Join[dsA,dsB,3] and then use the above Apply method, I get a Failure: KeyValueMap[Association[RowKeys->#1,#2]&] called with 2 arguments; 1 argument is expected. – KBL Aug 3 '19 at 1:51
• @KBL, posted what i get when I use Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]]  on dsC on v12 /Wolfram Cloud. – kglr Aug 3 '19 at 2:01
• you are right, I typed the wrong thing. What I meant to type was that the Apply method doesn't work when there are more than 2 rows--if I have dsD=Join[dsA,dsB] (without the 3) and type Apply[KeyValueMap[<|"RowKeys" -> #, #2|>&]]@dsD, I get KeyValueMap[Association[RowKeys->#1,#2]&] called with 2 arguments; 1 argument is expected. – KBL Aug 5 '19 at 16:01
• @KBL, looks like changing Apply to Map works for both cases. – kglr Aug 6 '19 at 16:14
• Thanks very much for the answers here and on the other question. As I posted on the other page, I found a nasty dataset wrinkle that was causing my dataset to be especially uncooperative. With your help, though, I have the answer. – KBL Aug 6 '19 at 18:39
    Merge[{Normal@dsA, Normal@dsB}, Join @@ # &] // Dataset