rduplicatesdata-manipulation# remove duplicate rows but one, and create a df with unique and duplicate rows with a column with count of duplicates in R

In R I have a dataframe, and I would like to have a new dataframe where a new last column has the count of how many identical rows there are of each row in the original dataframe. But with only one row for each. Perhaps I'm unable to briefly explain what I intend to do, but the dummy example should help explain it better. Please look at it.

Here is what I would like to do in R: I have a dataframe, say:

```
dat <- data.frame(x1 = c(1, 1, 2, 1, 4),
x2 = c(1, 1, 2, 1, 6),
x3 = c(2, 2, 3, 2, 6),
x4 = c(1, 1, 2, 2, 4),
x5 = c(1, 1, 4, 4, 3))
```

```
print(dat, row.names = FALSE)
```

```
x1 x2 x3 x4 x5
1 1 2 1 1
1 1 2 1 1
2 2 3 2 4
1 1 2 2 4
4 6 6 4 3
```

What I would like to achieve is a new data frame with:

```
x1 x2 x3 x4 x5 count
1 1 2 1 1 2
2 2 3 2 4 1
1 1 2 2 4 1
4 6 6 4 3 1
```

I tried to search the web and SO but was unable to find a solution. Can you please help ? Thank you in advance

ltdm

Solution

```
library(dplyr)
count(dat, across(x1:x5))
```

`x1 x2 x3 x4 x5 n 1 1 1 2 1 1 2 2 1 1 2 2 4 1 3 2 2 3 2 4 1 4 4 6 6 4 3 1`

- Select the data.frame with maximum dimensions from a list of data.frames
- nTrials must be be greater.... issue on conjoint design
- error in `na_if()`: ! Can't convert `y` <character> to match type of `x` <double>
- Getting errors installing glmnet package using R studio
- R plotly show only labels in the stacked bar chart where percentage value for the stack is above 5
- 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
- How to apply different functions for different groups and mutate?
- 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?
- How to plot a continuous color wheel with specific colors in ggplot2
- Retain unused factor levels in legend when using scale_*_manual
- Check values of column 1 present in values of column 2
- Conditional panel based on a selectInput with emoji
- 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