Hi Nargiz,
I've created a new reprex below which tries to solve your need by using two intermediate datasets. You can see that I also removed the sorting in descending order of the cost variable.
library(tidyverse)
data_NB <- tibble(client = c(1,1,1,2,2,3,3,4,5,5),
year = c(2000,2010,2000,2003,2007,2009,2009,2012,2017,2017),
type = c("A","A","B","B","C","D","D","A","C","D"),
cost = c(34,56,78,45,12,56,67,23,10,89)
)
# data_NB %>% head()
## keep rows which have multiple hits on client, year and *same* type
data_1 <- data_NB %>%
count(client, year, type) %>%
filter(n > 1) %>%
left_join(data_NB, by = c("client", "year", "type"))
data_1
#> # A tibble: 2 x 5
#> client year type n cost
#> <dbl> <dbl> <chr> <int> <dbl>
#> 1 3 2009 D 2 56
#> 2 3 2009 D 2 67
## remove rows which have different type
data_2 <- data_NB %>%
arrange(client, year, cost) %>%
distinct(client, year, .keep_all = TRUE)
data_2
#> # A tibble: 7 x 4
#> client year type cost
#> <dbl> <dbl> <chr> <dbl>
#> 1 1 2000 A 34
#> 2 1 2010 A 56
#> 3 2 2003 B 45
#> 4 2 2007 C 12
#> 5 3 2009 D 56
#> 6 4 2012 A 23
#> 7 5 2017 C 10
## merge both intermediate datasets into one.
data_2 %>%
full_join(data_1) %>%
arrange(client, year, type, cost) %>%
select(-n)
#> Joining, by = c("client", "year", "type", "cost")
#> # A tibble: 8 x 4
#> client year type cost
#> <dbl> <dbl> <chr> <dbl>
#> 1 1 2000 A 34
#> 2 1 2010 A 56
#> 3 2 2003 B 45
#> 4 2 2007 C 12
#> 5 3 2009 D 56
#> 6 3 2009 D 67
#> 7 4 2012 A 23
#> 8 5 2017 C 10
Created on 2021-02-11 by the reprex package (v1.0.0)
Be aware though that on this small example data set the result appear to be what you expect, but that in your dataset the result may differ: what happens when a client have multiple duplicate types in one year?
Anyway, I hope this helps you forward.