Skip to main content
Tweeted twitter.com/StackMma/status/918443707153690625
added 4 characters in body
Source Link
sebhofer
  • 2.8k
  • 18
  • 25

I have two datasets

ds = Dataset@*
   Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{
 {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10},
 {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9},
 {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12},
 {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15},
 {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12},
 {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

and

ds2 = Dataset@*
   Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{
 {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, 
 {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128},
 {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

and I combine it via

SortBy["ID"]@JoinAcross[dsds4=SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B, in line 5: 128 and lines 7-9: 10.

The result should look like this:

test

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], 
     prev = #"B"}[[1]] &]]

But it doesn't yield the desired result. It only replaces the first occurrence. Any ideas?

I have two datasets

ds = Dataset@*
   Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{
 {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10},
 {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9},
 {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12},
 {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15},
 {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12},
 {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

and

ds2 = Dataset@*
   Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{
 {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, 
 {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128},
 {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

and I combine it via

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B, in line 5: 128 and lines 7-9: 10.

The result should look like this:

test

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], 
     prev = #"B"}[[1]] &]]

But it doesn't yield the desired result. It only replaces the first occurrence. Any ideas?

I have two datasets

ds = Dataset@*
   Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{
 {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10},
 {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9},
 {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12},
 {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15},
 {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12},
 {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

and

ds2 = Dataset@*
   Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{
 {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, 
 {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128},
 {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

and I combine it via

ds4=SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B, in line 5: 128 and lines 7-9: 10.

The result should look like this:

test

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], 
     prev = #"B"}[[1]] &]]

But it doesn't yield the desired result. It only replaces the first occurrence. Any ideas?

fixed formatting
Source Link
sebhofer
  • 2.8k
  • 18
  • 25

ds = Dataset@* Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{ {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10}, {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9}, {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12}, {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15}, {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12}, {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

ds = Dataset@*
   Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{
 {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10},
 {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9},
 {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12},
 {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15},
 {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12},
 {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

ds2 = Dataset@* Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{ {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128}, {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

ds2 = Dataset@*
   Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{
 {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, 
 {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128},
 {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B. Line, in line 5: 128 and lines 7-9: 10...

...as (at the moment not) shown here The result should look like this:

still trouble with upload

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], prev = #"B"}[[1]] &]]

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], 
     prev = #"B"}[[1]] &]]

ds = Dataset@* Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{ {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10}, {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9}, {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12}, {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15}, {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12}, {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

ds2 = Dataset@* Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{ {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128}, {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B. Line 5: 128 and lines 7-9: 10...

...as (at the moment not) shown here

still trouble with upload

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], prev = #"B"}[[1]] &]]

ds = Dataset@*
   Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{
 {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10},
 {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9},
 {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12},
 {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15},
 {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12},
 {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}
ds2 = Dataset@*
   Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{
 {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, 
 {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128},
 {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}
SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B, in line 5: 128 and lines 7-9: 10.

The result should look like this:

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], 
     prev = #"B"}[[1]] &]]
added 63 characters in body
Source Link
Lea
  • 942
  • 4
  • 12

I have two datasets

ds = Dataset@* Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{ {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10}, {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9}, {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12}, {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15}, {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12}, {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

and

ds2 = Dataset@* Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{ {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128}, {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

and I combine it via

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B. Line 5: 128 and lines 7-9: 10...

...as (at the moment not) shown here

test

still trouble with upload

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], prev = #"B"}[[1]] &]]

But it doesn't yield the desired result. It only replaces the first occurrence. Any ideas?

I have two datasets

ds = Dataset@* Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{ {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10}, {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9}, {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12}, {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15}, {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12}, {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

and

ds2 = Dataset@* Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{ {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128}, {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

and I combine it via

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B. Line 5: 128 and lines 7-9: 10...

...as (at the moment not) shown here

still trouble with upload

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], prev = #"B"}[[1]] &]]

But it doesn't yield the desired result. It only replaces the first occurrence. Any ideas?

I have two datasets

ds = Dataset@* Map[AssociationThread[{"ID", "Date", "A"} -> #] &]@{ {"C12", SQLDateTime[{2012, 12, 11, 0, 0, 0.}], 10}, {"C12", SQLDateTime[{2015, 2, 19, 19, 30, 0.}], 9}, {"C12", SQLDateTime[{2013, 1, 22, 0, 0, 0.}], 12}, {"C10", SQLDateTime[{2013, 1, 28, 9, 25, 0.}], 15}, {"C10", SQLDateTime[{2014, 3, 21, 20, 0, 0.}], 12}, {"C11", SQLDateTime[{2014, 8, 27, 6, 40, 0.}], 8}}

and

ds2 = Dataset@* Map[AssociationThread[{"ID", "Date", "B"} -> #] &]@{ {"C10", SQLDateTime[{2013, 1, 28, 0, 25, 0.}], 315}, {"C11", SQLDateTime[{2013, 3, 2, 6, 40, 0.}], 128}, {"C12", SQLDateTime[{2012, 10, 10, 0, 0, 0.}], 10}}

and I combine it via

SortBy["ID"]@JoinAcross[ds, ds2, {"ID", "Date"}, "Outer"]

Now, I want to replace the unmatched B values with the last (according to date) occurring B value of the matching ID. Eg. in lines 2 and 3: 315 for B. Line 5: 128 and lines 7-9: 10...

...as (at the moment not) shown here

test

still trouble with upload

I tried something like this, where I first replaced the Missing["Unmatched"]->0 and then

Module[{prev = #"B" &}, ds4[All, {If[#"B" == 0, <|#, "B" -> #"B" + prev|>, #], prev = #"B"}[[1]] &]]

But it doesn't yield the desired result. It only replaces the first occurrence. Any ideas?

added 96 characters in body
Source Link
Lea
  • 942
  • 4
  • 12
Loading
deleted 2 characters in body
Source Link
Lea
  • 942
  • 4
  • 12
Loading
added 2 characters in body
Source Link
Lea
  • 942
  • 4
  • 12
Loading
Source Link
Lea
  • 942
  • 4
  • 12
Loading