Message Boards Message Boards

0
|
2061 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

How can I modify an element of a dataset?

I have been reading questions and answers about modifying a Dataset. However, it seems that everyone wants to add new columns, something I have done without a problem. However, I have a different problem. I read data from a CSV file that is simply the Date,Open,High,Low,Close and Volume (OHLCV) of a stock into a Dataset. Each row is a daily candle. ]

I send each row to a function expand[row_Dataset] which takes each original row and extends it with columns that are indicators to get a new type of row. So, if the indicators are Ind1 and Ind2, the newly created row has fields Date,Open,High,Low,Close,Ind1,Ind2, At first, the new indicators are empty, as they have not been calculated yet, The new rows are accumulated into a new Dataset that is just like the original one except that now each row of the new dataset consists of OHCV and Ind1 and Ind2.

So far so good. I have the original dataset and a new dataset made up of similar rows except that the new rows are 2 more fields (ind1 and ind2).

Here comes the problem. I pass the new Dataset as an argument to a function (call it f) whose purpose is to look at the new row and potentially change fields in previous rows! For instance, looking at the last row of the new dataset and the last 2 rows for a total of 3 rows, I might be able to find, for instance that the previous row had a peak. Say that Ind1 is a boolean (True/False) to let me know if that row has a peak (it had a peak higher than the previous row and the next/last row). I would like to change the value of that previous row's ind1 value to "True" to show that that row is a peak.

BUT I CAN'T! I understand that you cannot change the argument (original Dataset), which is why I made a copy of it in a local variable. I thought that I could do whatever I wanted to the local variable (e.g. modify any field, like set the Volume to 0 or Ind1 to True) and then return the new Dataset from my function and just set the original variable to the return value as in: originalDataset = f[originalDataset];

What am I missing?

POSTED BY: Henrick Jeanty

As I played more with my problem, I decided to convert my Dataset to its Normal form as a local Association variable in the function using Module so: f[dataset_Dataset] := Module[{localVar = dataset // Normal}, .......];

In this way, I now deal with an Association and I can modify whatever parts of the association I need to. Then, when I am done, I reconvert the modified Association to a Dataset with: Return[Dataset[localVar]];

Seems a bit of a roundabout way to solve my problem but that seems to work. Are there any suggestions for something simpler?

POSTED BY: Henrick Jeanty
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