Here is my test dataset :
test
date app count
1 21-10-18 A 2
2 21-10-19 A 3
3 21-10-23 A 5
4 21-10-19 B 2
5 21-10-24 B 3
I would like to group the data based on app column and replace the latest date by sys.Date
in the date column.
I could get the latest date per app by :
latest_date <- test %>% group_by(app)%>%slice(n())
latest_date
# A tibble: 2 x 3
# Groups: app [2]
date app count
<date> <chr> <int>
1 21-10-23 A 5
2 21-10-24 B 3
How could I find the row indexes in test
dataset which are match with the rows in latest_date
? (which (test == latest_date)) ????
what I need at the end is :
date app count
1 21-10-18 A 2
2 21-10-19 A 3
3 21-12-13 A 5
4 21-10-19 B 2
5 21-12-13 B 3
CodePudding user response:
Use replace
after doing a group by 'app'
library(dplyr)
library(lubridate)
test %>%
mutate(date = ymd(date)) %>%
group_by(app) %>%
mutate(date = replace(date, which.max(date), Sys.Date())) %>%
ungroup
-output
# A tibble: 5 × 3
date app count
<date> <chr> <int>
1 2021-10-18 A 2
2 2021-10-19 A 3
3 2021-12-13 A 5
4 2021-10-19 B 2
5 2021-12-13 B 3
data
test <- structure(list(date = c("21-10-18", "21-10-19", "21-10-23", "21-10-19",
"21-10-24"), app = c("A", "A", "A", "B", "B"), count = c(2L,
3L, 5L, 2L, 3L)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5"))