I'm trying to work on a data frame that I have, but am struggling to find the latest date associated with a name and the total sum associated with a name. My frame looks something like this
a<-
Date Name Sum
<date> <chr> <dbl>
23.02.2017 Johnny 6
24.02.2017 Jane 20
24.02.2017 Micky Mouse 20
27.02.2017 Jane 20
3.03.2017 Johnny 20
3.03.2017 Ronald 25
I would like to get something like this
b<-
Latest Date Name Frequency Total Sum
<date> <chr> <dbl> <dbl>
3.03.2017 Johnny 2 26
27.02.2017 Jane 2 40
24.02.2017 Micky Mouse 1 20
3.03.2017 Ronald 1 25
I Started by using the table function and then using a for loop, but I'm a bit of a noob.
b <- data.frame(table(a$Name))
# after cleaning
b<-
Name Frequency
<chr> <int>
Johnny 2
Jane 2
Micky Mouse 1
Ronald 1
for (i in (a$Name)) {
b <- a %>%
mutate(Total Sum = sum(a$Sum[a$Name == i] %>%
mutate(Latest Date = max(a$date[a$Name == i]))
}
This would return me a data frame that looks like this
b<-
Name Frequency Total Sum Latest Date
<chr> <int> <dbl> <date>
Johnny 2 40 27.02.2017
Jane 2 40 27.02.2017
Micky Mouse 1 40 27.02.2017
Ronald 1 40 27.02.2017
How can I make sure that the total sum is only that of Johnny, Jane ... and that the date is the latest date associated with that name