How to create a data frame from a database based on the difference of two dates in a column of another categorical variable in software R

0

I have the following database as an example and the result I expect:

In case the values in the new dataframe refer to the days, which is the difference of the last date of a category from the first of the same category.

    
asked by anonymous 17.04.2018 / 00:11

1 answer

3

You can do whatever you want with the base R function aggregate .

Grupo <- c("A", "A", "A", "B", "B", "C", "C")
Data <- c("01/02/2017", "15/02/2017", "20/03/2017", "18/02/2017", "01/03/2017", "15/02/2017", "20/02/2017")
dados <- data.frame(Grupo, Data)

dados$Data <- as.Date(dados$Data, "%d/%m/%Y")

result <- aggregate(Data ~ Grupo, dados, function(d) d[length(d)] - d[1])
result
#  Grupo Data
#1     A  47 
#2     B  11 
#3     C   5
    
17.04.2018 / 07:19