df.duplicated: Extract Duplicated or Unique Rows

View source: R/df.duplicated.R

df.duplicatedR Documentation

Extract Duplicated or Unique Rows

Description

The function df.duplicated extracts duplicated rows and the function df.unique extracts unique rows from a matrix or data frame.

Usage

df.duplicated(data, ..., margin = 1, first = TRUE, keep.all = TRUE,
              from.last = FALSE, keep.row.names = TRUE, check = TRUE)

df.unique(data, ..., margin =  1, keep.all = TRUE, from.last = FALSE,
          keep.row.names = TRUE, check = TRUE)

Arguments

data

a data frame.

...

an expression indicating the variable names in data used to determine duplicated or unique rows.e.g., df.duplicated(x1, x2, data = dat). Note that the operators ., +, -, ~, :, ::, and ! can also be used to select variables, see Details in the df.subset function.

margin

a numeric value indicating the margin, i.e., 1 (default) for extracting duplicated or unique rows and 2 for extracting duplicated or unique columns.

first

logical: if TRUE (default), the df.duplicated() function will return duplicated rows or columns including the first of identical rows or columns.

keep.all

logical: if TRUE (default), the function will return all variables in data after extracting duplicated or unique rows based on the variables specified in the argument .... Note that this argument applies only when margin = 1.

from.last

logical: if TRUE, duplication will be considered from the reversed side, i.e., the last of identical rows or columns would correspond to duplicated = FALSE. Note that this argument is only used when first = FALSE.

keep.row.names

logical: if TRUE (default), the row names from data are kept, otherwise they are set to NULL. Note that this argument applies only when margin = 1.

check

logical: if TRUE (default), argument specification is checked.

Details

Note that df.unique(x) is equivalent to unique(x). That is, the main difference between the df.unique() and the unique() function is that the df.unique() function provides the ... argument to specify a variable or multiple variables which are used to determine unique rows.

Value

Returns duplicated or unique rows or columns of the data frame specified in data.

Author(s)

Takuya Yanagida

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

df.check, df.head, df.tail, df.long, df.wide, df.merge, df.move, df.rbind, df.rename, df.round, df.sort, df.subset

Examples

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, 1, 6), x5 = c(1, 1, 4, 4, 3))

#————————————————————————————————————————————————————————————————————————————
# Duplicated Rows

# Example 1a: Extract duplicated rows based on all variables
df.duplicated(dat)

# Example 1b: Extract duplicated rows based on 'x1' and 'x4'
df.duplicated(dat, x1, x4)

# Example 1c: Exclude first of identical rows
df.duplicated(dat, first = FALSE)

# Example 1d: Do not return all variables
df.duplicated(dat, x2, x3, keep.all = FALSE)

# Example 1e: Consider duplication from the reversed side
df.duplicated(dat, x2, x4, first = FALSE, from.last = TRUE)

#————————————————————————————————————————————————————————————————————————————
# Duplicated Columns

# Example 2a: Extract duplicated columns
df.duplicated(dat, margin = 2)

# Example 2b: Exclude first of identical rows
df.duplicated(dat, margin = 2, first = FALSE)

# Example 2c: Consider duplication from the reversed side
df.duplicated(dat, margin = 2, first = FALSE, from.last = TRUE)

#—————————————————————————————————————————————————————————————————————————————
# Unique Rows

# Example 3a: Extract unique rows based on all variables
df.unique(dat)

# Example 3b: Extract unique rows based on 'x1', 'x2', and 'x3'
df.unique(dat, x1, x2, x3)

# Example 3c: Do not return all variables
df.unique(dat, x2, x3, keep.all = FALSE)

# Example 3d: Consider duplication from the reversed side
df.unique(dat, x1, from.last = TRUE)

#—————————————————————————————————————————————————————————————————————————————
# Unique Columns

# Example 4a: Extract unique columns based on all variables
df.unique(dat, margin = 2)

# Example 4b: Consider duplication from the reversed side
df.unique(dat, margin = 2, from.last = TRUE)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to df.duplicated in misty...