Add an "id" Variable to a Dataset

Share:

Description

Many functions will not work properly if there are duplicated ID variables in a dataset. This function is a convenience function for .N from the "data.table" package to create an .id. variable that when used in conjunction with the existing ID variables, should be unique.

Usage

1
Flag(.data, id.vars = NULL)

Arguments

.data

The input data.frame or data.table.

id.vars

The variables that should be treated as ID variables. Defaults to NULL, at which point all variables are used to create the new ID variable.

Value

The input dataset (as a data.table) if ID variables are unique, or the input dataset with a new column named ".ID".

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
df <- data.frame(A = c("a", "a", "a", "b", "b"),
                 B = c(1, 1, 1, 1, 1), values = 1:5);
df

df = Flag(df, c("A", "B"))

df <- data.frame(A = c("a", "a", "a", "b", "b"),
                   B = c(1, 2, 1, 1, 2), values = 1:5)
df
(df <- Flag(df, 1:2) )

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.