Skip to main content
Tweeted twitter.com/StackMma/status/740380436862504960
Found simpler way to do destructive FoldList
Source Link
Edmund
  • 44k
  • 3
  • 55
  • 152

I have a Dataset in the following form but with many more rows and a few other columns.

SeedRandom[123]; ds = Dataset[ AssociationThread[{"Date", "R1","R2"}, {DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}], Sequence @@ RandomReal[{-0.5, 1}, 2]} ] & /@ Range[3] ] 

enter image description here

I would like to apply Rest@FoldList[#1 (1 + #2/100) &, 100, #] & to columns "R1" and "R2" to get the following.

enter image description here

Currently I am destroying the dataset and then recreating it to get this result.

Dataset[ AssociationThread[{"Date", "R1", "R2"}Dataset@MapThread[Append, #] & /@ Transpose[ Prepend[ {<|"Date" -> DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}]]|> & /@  Range[3]]@ ds[Transpose /* Range[3],   Map[Rest@FoldList[#1Normal@ds[Rest@FoldList[#1 (1 + #2/100) &, 100, #] &]&, {"R1", "R2"} /* Values] ]]]}] 

However, I would like not to have to do this because the columns that do not need to be folded need to be reincorporated into the result (e.g."Date" in the example). The ds[All, {"R1" -> f, "R2" -> f}] syntax operates on each column's row values individually. Is there some similar syntax for folding a column? Or a non destructive way of doing this?

Ideally I am looking to get the following but I can prepend the starting 100 value and date to the dataset if I can get the above.

enter image description here

I have a Dataset in the following form but with many more rows and a few other columns.

SeedRandom[123]; ds = Dataset[ AssociationThread[{"Date", "R1","R2"}, {DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}], Sequence @@ RandomReal[{-0.5, 1}, 2]} ] & /@ Range[3] ] 

enter image description here

I would like to apply Rest@FoldList[#1 (1 + #2/100) &, 100, #] & to columns "R1" and "R2" to get the following.

enter image description here

Currently I am destroying the dataset and then recreating it to get this result.

Dataset[ AssociationThread[{"Date", "R1", "R2"}, #] & /@ Transpose[ Prepend[  DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}] & /@  Range[3]]@ ds[Transpose /*    Map[Rest@FoldList[#1 (1 + #2/100) &, 100, #] &], {"R1", "R2"} /* Values] ]] 

However, I would like not to have to do this because the columns that do not need to be folded need to be reincorporated into the result (e.g."Date" in the example). The ds[All, {"R1" -> f, "R2" -> f}] syntax operates on each column's row values individually. Is there some similar syntax for folding a column? Or a non destructive way of doing this?

Ideally I am looking to get the following but I can prepend the starting 100 value and date to the dataset if I can get the above.

enter image description here

I have a Dataset in the following form but with many more rows and a few other columns.

SeedRandom[123]; ds = Dataset[ AssociationThread[{"Date", "R1","R2"}, {DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}], Sequence @@ RandomReal[{-0.5, 1}, 2]} ] & /@ Range[3] ] 

enter image description here

I would like to apply Rest@FoldList[#1 (1 + #2/100) &, 100, #] & to columns "R1" and "R2" to get the following.

enter image description here

Currently I am destroying the dataset and then recreating it to get this result.

Dataset@MapThread[Append, {<|"Date" -> DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}]|> & /@ Range[3], Normal@ds[Rest@FoldList[#1 (1 + #2/100) &, 100, #] &, {"R1", "R2"}]}] 

However, I would like not to have to do this because the columns that do not need to be folded need to be reincorporated into the result (e.g."Date" in the example). The ds[All, {"R1" -> f, "R2" -> f}] syntax operates on each column's row values individually. Is there some similar syntax for folding a column? Or a non destructive way of doing this?

Ideally I am looking to get the following but I can prepend the starting 100 value and date to the dataset if I can get the above.

enter image description here

Source Link
Edmund
  • 44k
  • 3
  • 55
  • 152

How to FoldList over Dataset column without destroying/rebuilding Dataset?

I have a Dataset in the following form but with many more rows and a few other columns.

SeedRandom[123]; ds = Dataset[ AssociationThread[{"Date", "R1","R2"}, {DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}], Sequence @@ RandomReal[{-0.5, 1}, 2]} ] & /@ Range[3] ] 

enter image description here

I would like to apply Rest@FoldList[#1 (1 + #2/100) &, 100, #] & to columns "R1" and "R2" to get the following.

enter image description here

Currently I am destroying the dataset and then recreating it to get this result.

Dataset[ AssociationThread[{"Date", "R1", "R2"}, #] & /@ Transpose[ Prepend[ DatePlus[DateObject@{2007, 12, 31}, {#, "EndOfMonth"}] & /@ Range[3]]@ ds[Transpose /* Map[Rest@FoldList[#1 (1 + #2/100) &, 100, #] &], {"R1", "R2"} /* Values] ]] 

However, I would like not to have to do this because the columns that do not need to be folded need to be reincorporated into the result (e.g."Date" in the example). The ds[All, {"R1" -> f, "R2" -> f}] syntax operates on each column's row values individually. Is there some similar syntax for folding a column? Or a non destructive way of doing this?

Ideally I am looking to get the following but I can prepend the starting 100 value and date to the dataset if I can get the above.

enter image description here