From discrete dates to continuous dates (revised)

I have a timetable T like
city date visitors
-----------------------
NY January 1 2020 100
NY January 5 2020 40
NY January 12 2020 300
SF January 1 2020 70
SF January 6 2020 240
SF January 15 2020 30
I want to embed this in a larger table with continuous dates such as
city date visitors
-----------------------
NY January 1 2020 100
NY January 2 2020 NaN
NY January 3 2020 NaN
NY January 4 2020 Nan
NY January 5 2020 40
...
How can one do this?

3 个评论

https://www.mathworks.com/matlabcentral/answers/167799-inserting-missing-dates-and-values
Can you upload the table you have in a MAT file, so that we don't have to guess at the date format?
Also, am I to understand that you do not want "2020" to appear in the added dates?
Thank you. They are timetables. I have edited the question text.

请先登录,再进行评论。

 采纳的回答

Here's a sample timetable.
dt = datetime(2020, 1, [1; 5; 12]);
T = timetable(dt, [100; 40; 300], 'VariableNames', "visitors");
T.Properties.DimensionNames{1} = 'date'
T = 3x1 timetable
date visitors ___________ ________ 01-Jan-2020 100 05-Jan-2020 40 12-Jan-2020 300
retime it either filling rows not appearing in the original with missing (the default) or with the constant 0
T_fillWithMissing = retime(T, 'daily')
T_fillWithMissing = 12x1 timetable
date visitors ___________ ________ 01-Jan-2020 100 02-Jan-2020 NaN 03-Jan-2020 NaN 04-Jan-2020 NaN 05-Jan-2020 40 06-Jan-2020 NaN 07-Jan-2020 NaN 08-Jan-2020 NaN 09-Jan-2020 NaN 10-Jan-2020 NaN 11-Jan-2020 NaN 12-Jan-2020 300
T_fillWith0 = retime(T, 'daily', 'fillwithconstant', 'Constant',0)
T_fillWith0 = 12x1 timetable
date visitors ___________ ________ 01-Jan-2020 100 02-Jan-2020 0 03-Jan-2020 0 04-Jan-2020 0 05-Jan-2020 40 06-Jan-2020 0 07-Jan-2020 0 08-Jan-2020 0 09-Jan-2020 0 10-Jan-2020 0 11-Jan-2020 0 12-Jan-2020 300

1 个评论

Thank you. I have edied the question that generalizes the previous question.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Reference Applications 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by