Notification of a date for another R

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 show the person's name when she turns 24. For example, when it is 90 days before it is 24, I have to show the name, how can I do that?

I'm using the R language

    
asked by anonymous 24.09.2018 / 18:24

1 answer

4

I do not deal with RStudio and Shiny, I do not know what automation would be like in the one you are developing, but here's how you can do it. I've modified your sample data to have more than one birthday person and a 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 the days to date format. I also find it more practical to have the names as text strings rather than factors. You can establish these formats directly when you are reading your data.

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

The Sys.Date function takes the system date. You can use it to calculate the difference in days between the current date and the date of birth. I imagine you will also need a comparison to exclude people whose 24-year birthdays have 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
    
24.09.2018 / 19:28