I have two dynamic dates:
"A5" - 2018-12-01
"B5" - 2019-04-31
I would like something that would help me list all the months along with their specific years that are between both those dates (including both of them).
The output would be:
December 2018
January 2019
February 2019
March 2019
April 2019
I need to do this without a Script. These dates difference are never going to be bigger than 2 years.
Is there a way to do this that you know of? I'm kind of stuck.
=ARRAYFORMULA(TEXT(UNIQUE(EOMONTH(ROW(INDIRECT(
DATEVALUE(A5)&":"&DATEVALUE(B5))), 0)), "mmmm yyyy"))
note: 2019-04-31 is not a valid date
Date(year(B5),month(B5),1)
Date(year(B5),month(B5)-1,1)
Date(year(B5),month(B5)-2,1)
I repeated this formula only changing the month part, which gives a date list. Then, with a helper column, I used, if(thenewcells>=date(year(A5),month(A5),1,"include","")
This lets me know which ones to include.
However, I need to create these in advance, not dynamic
Related
I have a dataset for a time series spanning a couple of years with daily observations. I'm trying to smooth some clearly wrong data inserted there (for example, negative values when the variable cannot take values below zero) and what I came up with was trying to smooth it or "interpolate" it by using both the mean of the days around that observation and the mean of the same day or couple of days from previous years, as I have yearly seasonality (I'm still unsure about this part, any comment would be greatly appreciated).
So my question is whether I can easily access the same day acrosss different years.
Here's a dummy example of my data:
library(tidyverse)
library(lubridate)
date value
2016-10-01 00:00:00 28
2016-10-02 00:00:00 25
2016-10-03 00:00:00 24
2016-10-04 00:00:00 22
2016-10-05 00:00:00 -6
2016-10-06 00:00:00 26
I have that for years 2016 through 2020. So in this example I would use the dates around 2016-10-05 AND I would like to use the dates around the 5th of October from years 2017 to 2020 to kind of maintain the seasonality, but maybe this is incorrect.
I tried to use +years() from lubridate but I still have to do things manually and I would like to kind of autimatize things.
If your question is solely "whether [you] can easily access the same day [across] different years", you could do that as follows:
# say your data frame is called df
library(lubridate)
day(df$date)
This will return the day part of the date for every entry in that column of your data frame.
Edit to reply to comment from asker:
This is a very basic way to specify the day and month for which you would like to obtain the corresponding rows in your data frame:
df[day(df$dates) == 5 & month(df$dates) == 10, ]
I have longitudinal data in a data frame in long format in R, such that a person can be present on several rows, where each row has a specific date - but never the same date. Data is sorted by personal ID firstly and secondly by date, such that early dates for an individual comes first.
Following is what I would like to accomplish:
The first date for each individual should be kept. For the rest of the dates I want to remove all dates occurring within 30 days of a previous date for that person. But, if a row is removed, no other following dates should be compared to that date. The dates should be removed in order, from top to bottom. I.e. if a person has dates 14 May 2020, 20 May 2020, 22 May 2020 and 17 June 2020 I would like to remove the rows in the data frame with the two middle dates, as they are close to the first date: 14 May 2020. I have been able to do this with for loops, but it is not at all time efficient for big data. Does anybody know how I could solve this in a better way?
I'm trying to get date for first day of the new year, it means that I tried something like this:
dateFrom = moment().month(0).day(01).format('YYYY-MM-DD');
But it gives me date:
2013-12-30
Instead of the
2014-01-01
How can I solve it please?
Thanks for any advice.
You are looking for date instead of day if you want to define the day of the month.
This works:
moment().month(0).date(1).format('YYYY-MM-DD');
The date method defines day of the month, docs here.
The day method defines the day of the week. From the docs:
So, by using day(1) you are asking to get the nearest Monday. In your case the nearest Monday to January 1st, 2014 is December 30th, 2013
I have an excel sheet that basically looks like this:
| A | B
1 | 23.12.2013 | 03.01.2014
2 | 20.01.2014 | 25.01.2014
and so forth. The dates stored in start and end could be in the same month but are not necessarily. Simply summing up workdays in a range is not that hard: =NETWORKDAYS(A1;B1) is fine here.
So the question is how do I sum up the work days in a range that belong to a certain month? For the first row for example the result would be 7 for December and 3 for January.
For example, if your bounds are in cells A1 and A2 and you want to intersect the range with December 2013:
=NETWORKDAYS(MAX(A1,DATE(2013,12,1)),MIN(A2,DATE(2013,12,31)))
Using #Taosique's formula (+1) and modifying it a little, you can create a table (which will have to be maintained as and when required) to track such things down.
I would create several columns with headers containing dates, being 1st December 2013, then 1st January 2014, 1st February 2014, etc.
Then in cell C2, use the formula:
=IFERROR(MAX(NETWORKDAYS(MAX($A2,C$1),MIN(D$1-1,$B2)),0),0)
Then drag it down and across to fill for the other columns (months) and people. See the google spreadsheet I made here if you wan to see it.
Google Spreadsheet has some limitations on formatting, but you can 'hide' the date of the column by turning it into a month by formatting it as mmm-yy in Excel.
I have a set of events that each have a start and end date, but they take place over the scope of a number of months. I would like to create a table that shows the number of days in each month for this event.
I have the following example.
event_start_date <- as.Date("23/10/2012", "%d/%m/%Y")
event_end_date <- as.Date("07/02/2013", "%d/%m/%Y")
I would expect to get a table out as the following:
Oct-12 8
Nov-12 30
Dec-12 31
Jan-13 31
Feb-13 7
Does anybody know about a smart and elegant way of doing this or is creating a system of loops the only viable method?
Jochem
This is not necessarily efficient because it creates a sequence of days, but it does the job:
> library(zoo)
> table(as.yearmon(seq(event_start_date, event_end_date, "day")))
Oct 2012 Nov 2012 Dec 2012 Jan 2013 Feb 2013
9 30 31 31 7
If your time span is so large than this method is slow, you'll have to create a sequence of firsts of the months between your two (truncated) dates, take the diff, and do a little extra work for the end points.
As DjSol already pointed out in his comment, you can just subtract two dates to get the number of days:
event_start_date <- as.Date("23/10/2012", "%d/%m/%Y")
event_end_date <- as.Date("07/02/2013", "%d/%m/%Y")
as.numeric(event_end_date - event_start_date)
Is that what you want? I have the feeling that you might have more of a problem to get the start and end date in such a format so you can easily subtract them because you mention a loop. If so, however, I guess we need more details on how your actual data looks.