# Select Unique Rows in a Data Frame in R

Select Unique Rows in a Data Frame in R, R is a popular programming language that is widely used for statistical computing and graphics.

One of the most common tasks in data analysis is to select unique rows from a large dataset. In this tutorial, we will learn how to select unique rows in a data frame using R.

A data frame is a rectangular data structure in R that contains multiple variables or columns. Each row in the data frame represents an observation or record, and each column represents a variable or feature.

In some cases, we may have duplicate rows in our data frame, which can lead to issues in further analysis or visualization.

To avoid these issues, we can select unique rows from the data frame using R’s built-in functions. The following R functions can be used to pick distinct rows from a data frame:

Using the provided data frame, this tutorial walks through the practical application of each method:

```df <- data.frame(team=c('A1', 'A1', 'A1', 'A1', 'B1', 'B1', 'B1', 'B1'),
position=c('LG', 'LG', 'LF', 'GF', 'GG', 'GG', 'FF', 'FF'),
points=c(100, 100, 18, 104, 105, 105, 107, 107))
df

team position points
1   A1       LG    100
2   A1       LG    100
3   A1       LF     18
4   A1       GF    104
5   B1       GG    105
6   B1       GG    105
7   B1       FF    107
8   B1       FF    107```

### Example 1: Choose Unique Rows in Every Column

The code that follows demonstrates how to choose rows in the data frame with distinct values in every column:

How Do Machine Learning Chatbots Work » Data Science Tutorials

```library(dplyr)

#select rows with unique values across all columns
df %>% distinct()

team position points
1   A1       LG    100
2   A1       LF     18
3   A1       GF    104
4   B1       GG    105
5   B1       FF    107```

It is evident that the data frame has five distinct rows.

Note: Only the first unique row is retained when duplicate rows are found.

### Example 2: Choose Identical Rows Using Just One Column

To choose distinct rows solely based on the team column, use the code that follows.

```library(dplyr)

#select rows with unique values based on team column only
df %>% distinct(team, .keep_all=TRUE)

team position points
1   A1       LG    100
2   B1       GG    105```

Only the rows containing each value’s initial occurrence are retained because the team field only contains two unique values.

Note: R is instructed to retain all additional columns in the output by the argument.keep_all=TRUE.

### Example 3: Choose Distinct Rows Using Several Columns

The code that follows demonstrates how to choose distinct rows just using the team and position columns.

```library(dplyr)

#select rows with unique values based on team and position columns only
df %>% distinct(team, position, .keep_all=TRUE)

team position points
1   A1       LG    100
2   A1       LF     18
3   A1       GF    104
4   B1       GG    105
5   B1       FF    107```

Given that the team and position columns contain four distinct sets of data, four rows are returned.

Sample Size Calculation and Power Clinical Trials » finnstats