Notification from one date to another R

Asked

Viewed 56 times

2

DF = data

  idade dt          nome
    2   02-02-2016  Marq
    8   05-05-2010  Andre
    24  06-06-1994  Sudre
    23  25-05-1995  Jaq

This is my df, I need to get the name of the person when she turns 24. For example, when she’s 90 days away from being 24, I have to show her the name, how can I do it ?

I’m using the language R

1 answer

4


I don’t deal with Rstudio and Shiny, I don’t know what automation would be like within what you’re developing, but here’s how you can do the math. I modified your sample data to have more than one birthday boy and one person over 24:

dados <- read.table(text = '
  idade dt          nome
    2   02-02-2016  Marq
    8   05-05-2010  Andre
    24  06-10-1994  Sudre
    24  25-10-1994  Jaq
    25  06-06-1993  Paulo',
  header = TRUE
)

First convert days to date format. I also find it more practical to have names as text strings rather than factors. You can set these formats directly when reading your data.

dados$dt <- as.Date(dados$dt, format = '%d-%m-%Y')
dados$nome <- as.character(dados$nome)

The function Sys.Date takes the date of the system. You can use it to calculate the difference of days between the current date and the date of birth. I imagine you’ll also need a comparison to exclude people whose 24th birthday has passed.

aniversariantes <- 
  dados[
    Sys.Date() - dados$dt > 24*365.25 - 90 &
    Sys.Date() - dados$dt < 24*365.25 + 1,
    'nome']

cat( c( 
  'Faltam menos de 90 dias para o(s) aniversário(s) de 24 anos de',
  paste(aniversariantes, collapse = ' e ')
) )

Faltam menos de 90 dias para o(s) aniversário(s) de 24 anos de Sudre e Jaq
  • A doubt, pq put >(24365.25-90) and <(24365.25+1)

  • 1

    'Date' works with number of days. 1 year = 365 days, but we have leap years every four years, so a better account is multiply by 365.25. You want to identify when it’s 90 days before the birthday, then -90. You can use == to have only exactly when there are 90 days left. Assuming that no one checks every day, it is possible to pass the date; in that case use > It means "90 days or less". But in this case we must also set an upper limit, which corresponds to anyone who has passed 24 years. They’re just ideas, see what suits you best.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.