rdplyrduplicatesaggregatedata-transform# r retain duplicates after group by not min value

I have a dataset like this.

```
ID Group Value Col3
1 z1 1.29 1
1 z1 0.81 1
2 z2 2.89 1
2 z2 1.53 2
3 z1 0.13 3
3 z1 0.97 3
4 z3 10.75 3
4 z3 8.13 2
5 x2 0.45 1
5 x2 1.43 3
```

How do I retain rows where `Col3=2`

when duplicates are identified based on `group_by(Id,Group)`

Expected results

```
ID Group Value Col3
1 z1 1.29 1
1 z1 0.81 1
2 z2 1.53 2
3 z1 0.13 3
3 z1 0.97 3
4 z3 8.13 2
5 x2 0.45 1
5 x2 1.43 3
```

Please note that one row in ID 2 and ID 4 are exclude and only rows where Col3=2 is retained. Thanks in advance for any help.

Solution

It seems if there is a 2 in a group you want to keep just that, otherwise return all rows. This seems to do the trick

```
dd %>%
group_by(ID, Group) %>%
filter(!any(Col3==2) | (any(Col3==2) & Col3==2))
```

which returns

```
ID Group Value Col3
<int> <chr> <dbl> <int>
1 1 z1 1.29 1
2 1 z1 0.81 1
3 2 z2 1.53 2
4 3 z1 0.13 3
5 3 z1 0.97 3
6 4 z3 8.13 2
7 5 x2 0.45 1
8 5 x2 1.43 3
```

- ggplot labels with "K" for thousands or "M" for millions (maintaining 'comma' y-axis labeling)
- Automated procedure to draw ray in ggplot
- group_by and pivot_wider combination
- lm(): What is qraux returned by QR decomposition in LINPACK / LAPACK
- R studio time column is between
- Optimizing nested ifelse in shiny
- Expanding dataset rows based on intervals in R
- Free scales for facets in facet_grid change pattern
- Sort a factor based on value in one or more other columns
- SQL query on arrow duckdb workflow in R
- Rolling left join with value fill for all the rows in the left table
- GLM object in R takes more RAM than the object size of the GLM object
- Can you set an inferior limit to scale breaks on ggplot with facet_wrap?
- Retain unused factor levels in legend when using scale_*_manual
- Check values of column 1 present in values of column 2
- Scraping specific elements from various websites and saving them in the same vector
- Custom replacement matrix for edit distance in R
- How to remove duplicate rows in R?
- Passing a class(column) value as a string to process imbalance data(caret::ROSE)
- Add a common ylab to patchwork plots in r
- Heatmap of matrix with 2 variables in upper and lower triangles using R ggplot2
- ggplot2: creating double bar plot with one column representing x and y
- Identify connected subnetworks, constrained by edge attributes
- how to display factor in scientific notation
- Map one column's distinct values to the count of those values in a chart
- Simple Function in R error
- Using quotation marks in an R function
- calculating percentage change in a for loop
- How can I count and return which strings in a list have been detected using str_detect?
- create new columns based on the column names in tidyverse