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.
0 个评论
回答(1 个)
Sonam Gupta
2017-10-26
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!