I start with some data
datos <- read.table(text = '
fecha edad
01/05/2020 20
02/01/2019 33
03/05/2017 19
04/05/2018 3
09/05/2020 25
', header = TRUE, stringsAsFactors = FALSE)
datos$fecha <- as.Date(datos$fecha, format="%d/%m/%Y")
I want to compare the data date with a reference date.
fecha_referencia <- as.Date("25/11/2019", format="%d/%m/%Y")
I want you to notify me if there is a date less than the reference date. Something like
ifelse (datos$fecha < fecha_referencia , "Menor a la fecha","Mayor a la fecha")
But this gives 5 messages, one per case. I'm looking for you to notify me once. On plan:
"There are dates prior to the reference date, they are the ones in the position"
which(datos$fecha < fecha_referencia)
or if there were none:
"All dates are after the reference date"
What I'm looking for is that the message only goes out once.
You have two possible conditions: either the list of minor values is null or you have 1 or more values less than the reference value, in the latter case a "concatenation" would have to be shown and not the values separately.
It is not bad policy to ensure a maximum limit when one translates a set of "vertical" values to a single "horizontal" one:
One way that occurs to me is to store the which in a variable for example "check", which contains the positions that meet the condition
Then concatenate the rows into a vector of length 1, so that the message appears only once
Finally throw the message:
Considering Patrick's excellent advice, you can create a function for every event (including equal dates). The argument
limite
will determine the number of dates to print for each condition.