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?

조회 수: 1 (최근 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일
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
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 CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by