I have data in a bad format that I want to make tidy using an R script. I lack the skills to convert it into the format I want, so I look for an answer that (a) outlines a method, or (b) hands me the entire script.
The definition of tidy here is that the output has the following columns:
swap_id
a unique identifier.leg
either pay or rec.cashflow
the amount that is recieveddate
the date the cashflow arrivestype
whether the cashflow is of INTEREST of FINAL_EX.
The variable leg
takes on two different values: pay and rec. Currently leg
=pay is in one excel sheet and leg
=rec is in a second excel sheet. The entire excel workbook hence contains two sheets.
I have uploaded the entire excel file to https://we.tl/V9C5iVMH4w
Here is a sample of the content of sheet "rec":
id1 id2
date cf type date cf type
2017-04-04 42961 INTEREST 2015-04-07 33953 INTEREST
2017-07-04 43438 INTEREST 2016-04-04 203161 INTEREST
2017-10-04 43915 INTEREST 2017-04-04 203161 INTEREST
2018-01-04 43915 INTEREST 2018-04-04 203161 INTEREST
2018-04-04 42961 INTEREST 2019-04-04 203161 INTEREST
2018-07-04 43438 INTEREST 2020-04-06 203161 INTEREST
2018-10-04 43915 INTEREST 2021-04-06 203161 INTEREST
2019-01-04 43915 INTEREST 2022-04-04 203161 INTEREST
2019-04-04 42961 INTEREST 2023-04-04 203161 INTEREST
2019-07-04 43438 INTEREST 2023-04-04 5016330 FINAL_EX
2019-10-04 43915 INTEREST
2020-01-07 43915 INTEREST
2020-04-06 43438 INTEREST
2020-07-06 43438 INTEREST
2020-10-05 43915 INTEREST
2021-01-04 43915 INTEREST
2021-04-06 42961 INTEREST
2021-07-05 43438 INTEREST
2021-10-04 43915 INTEREST
2021-10-04 2988563 FINAL_EX
Here is a sample of the content of sheet "pay":
id1 id2
date cf type date cf type
2017-04-04 5250 INTEREST 2015-04-07 30938 INTEREST
2017-07-04 5308 INTEREST 2016-04-04 30938 INTEREST
2017-10-04 5367 INTEREST 2017-04-04 30938 INTEREST
2018-01-04 5367 INTEREST 2018-04-04 30938 INTEREST
2018-04-04 5250 INTEREST 2019-04-04 30938 INTEREST
2018-07-04 5308 INTEREST 2020-04-06 30938 INTEREST
2018-10-04 5367 INTEREST 2021-04-06 30938 INTEREST
2019-01-04 5367 INTEREST 2022-04-04 30938 INTEREST
2019-04-04 5250 INTEREST 2023-04-04 30938 INTEREST
2019-07-04 5308 INTEREST 2023-04-04 540000 FINAL_EX
2019-10-04 5367 INTEREST
2020-01-06 5367 INTEREST
2020-04-06 5308 INTEREST
2020-07-06 5308 INTEREST
2020-10-05 5367 INTEREST
2021-01-04 5367 INTEREST
2021-04-06 5250 INTEREST
2021-07-05 5308 INTEREST
2021-10-04 5367 INTEREST
2021-10-04 315000 FINAL_EX
In other words, each id gets itws own little table. An every table is separated using an empty column. This is good for eye balling the data, but horrible for working with it.
Here is how I want the output to be structured after the transformation.
swap_id leg date cf type
id1 pay 2017-04-04 5250 INTEREST
id1 pay 2017-07-04 5308 INTEREST
id1 pay 2017-10-04 5367 INTEREST
id1 pay 2018-01-04 5367 INTEREST
id1 pay 2018-04-04 5250 INTEREST
id1 pay 2018-07-04 5308 INTEREST
id1 pay 2018-10-04 5367 INTEREST
id1 pay 2019-01-04 5367 INTEREST
id1 pay 2019-04-04 5250 INTEREST
id1 pay 2019-07-04 5308 INTEREST
id1 pay 2019-10-04 5367 INTEREST
id1 pay 2020-01-06 5367 INTEREST
id1 pay 2020-04-06 5308 INTEREST
id1 pay 2020-07-06 5308 INTEREST
id1 pay 2020-10-05 5367 INTEREST
id1 pay 2021-01-04 5367 INTEREST
id1 pay 2021-04-06 5250 INTEREST
id1 pay 2021-07-05 5308 INTEREST
id1 pay 2021-10-04 5367 INTEREST
id1 pay 2021-10-04 315000 FINAL_EX
id1 rec 2017-04-04 42961 INTEREST
id1 rec 2017-07-04 43438 INTEREST
id1 rec 2017-10-04 43915 INTEREST
id1 rec 2018-01-04 43915 INTEREST
id1 rec 2018-04-04 42961 INTEREST
id1 rec 2018-07-04 43438 INTEREST
id1 rec 2018-10-04 43915 INTEREST
id1 rec 2019-01-04 43915 INTEREST
id1 rec 2019-04-04 42961 INTEREST
id1 rec 2019-07-04 43438 INTEREST
id1 rec 2019-10-04 43915 INTEREST
id1 rec 2020-01-07 43915 INTEREST
id1 rec 2020-04-06 43438 INTEREST
id1 rec 2020-07-06 43438 INTEREST
id1 rec 2020-10-05 43915 INTEREST
id1 rec 2021-01-04 43915 INTEREST
id1 rec 2021-04-06 42961 INTEREST
id1 rec 2021-07-05 43438 INTEREST
id1 rec 2021-10-04 43915 INTEREST
id1 rec 2021-10-04 2988563 FINAL_EX
id2 … … … … and so on...