How can I add a column to a table where I look up the date against a list of holiday dates and if it is a holiday I create a new binary variable?

3 ビュー (過去 30 日間)
I have a file with data indexed by dates. The first column in the table in dates. I have another table of a list of US bank holidays. I want to look up the dates in my data file and create a new binary variable in the data table such that if it is a holiday I mark the variable 1 and if it is not a holiday I mark it 0.

回答 (1 件)

Sonam Gupta
Sonam Gupta 2017 年 10 月 26 日
Based on the description of your question, I have created a demo for you.
%dates in November month
t1 = datetime(2017, 10,1, 8, 0, 0);
t2 = datetime(2017, 10, 30, 8, 0, 0);
t = t1:t2;
t = t';
%serial number
a = 1:30;
a = a';
%generating table 1 with list of dates
mytable = table(a, t);
%generating holiday tables
hol = t1:caldays(5):t2;
holiday_table = table(hol');
%finding common dates between the two tables
d1 = datetime(mytable{:,2});
d2 = datetime(holiday_table{:,1});
common_dates = intersect (d1, d2);
rows = ismember(mytable.t, common_dates);
%concatenate it with the existing table
mytable = [mytable table(rows)];
I hope this helps.
  1 件のコメント
Peter Perkins
Peter Perkins 2017 年 11 月 16 日
I think it's simpler than that: if holidays is a datetime vector of holidays, then
mytable.isHoliday = ismember(mytable.Date,holidays)
should do it.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by