Going back in time 1 week or a year
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dear all,
I attach a data set of weekly observations on an index.
Suppose that I am in week 2023/6/16 (yellow cell). For this cell I want to obtain the corresponding index value
Then I want to go back one week (7 days) ; that is to 2023/6/9 (yellow cell) and calculate its corresponding index value and then calculate the percentage change between the two values.
I used something like
t = datetime(2023,6,16);
t2 = dateshift(t,'dayofweek', 7,'previous')
but it does not work. I am sure that there is a mistake in my code. Is there a way to do that.
Also, assuming that we are in week 2023/6/16, I want to calculate the mean, min, max of the index values for the last 12 months. Is there a way to do that?
I would very much appreciate some guidance.
Thank you in advance!
0 comentarios
Respuesta aceptada
Voss
el 14 de Jul. de 2023
Editada: Voss
el 17 de Jul. de 2023
T = readtable('data.xlsx')
"Suppose that I am in week 2023/6/16 (yellow cell). For this cell I want to obtain the corresponding index value
Then I want to go back one week (7 days) ; that is to 2023/6/9 (yellow cell) and calculate its corresponding index value and then calculate the percentage change between the two values."
dt = datetime(2023,6,9); % June 9th, 2023
row_dt = find(T.Date == dt);
row_a_week_before_dt = find(T.Date == dt - caldays(7));
Index_dt = T{row_dt,'Index'}
Index_a_week_before_dt = T{row_a_week_before_dt,'Index'}
percent_change = 100*(Index_dt-Index_a_week_before_dt)/Index_a_week_before_dt
"Also, assuming that we are in week 2023/6/16, I want to calculate the mean, min, max of the index values for the last 12 months. Is there a way to do that?"
dt = datetime(2023,6,16); % June 16th, 2023
row_dt = find(T.Date == dt);
row_a_year_before_dt = find(T.Date > dt - calyears(1), 1);
Index_last_year = T{row_a_year_before_dt:row_dt,'Index'};
mean_last_year = mean(Index_last_year)
min_last_year = min(Index_last_year)
max_last_year = max(Index_last_year)
2 comentarios
Peter Perkins
el 17 de Jul. de 2023
This answer is incorrect. Do NOT subtract days or years for this purpose.
As the doumentation says, these are "exact-length time" units equal to exactly 24hrs and 365.2425*24hrs. You may be using unzoned datetimes today, but eventually, you will use time zones, in which case days will give you the wrong answer.
years will already give you the wrong answer.
Use caldays and calyears, as Steve Lord says.
I also recommend that you use timetables, and look into things like groupsummary or retime, to compute data summaries over each week, or year or whatever. Your life will be much easier.
Más respuestas (1)
Steven Lord
el 15 de Jul. de 2023
Subtract the appropriate calendar duration.
t = datetime('today')
oneWeekAgo = t - calweeks(1)
oneYearAgo = t - calyears(1)
Don't try to subtract 1 year as a duration. That's 365.24 days, not exactly 1 year.
oneYearAgoAlmost = t - years(1)
0 comentarios
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!