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 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Sonam Gupta
on 26 Oct 2017
Hi,
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 Comment
Peter Perkins
on 16 Nov 2017
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.
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!