Learn R Programming

splitstackshape (version 1.2.0)

getanID: Add an "id" variable to a dataset

Description

Many functions will not work properly if there are duplicated ID variables in a dataset. This function is a convenience function for using ave and seq_along to create an .id variable that when used in conjunction with the existing ID variables, should be unique.

Usage

getanID(data, id.vars)

Arguments

data
The input data.frame.
id.vars
The variables that should be treated as ID variables.

Value

  • The input data.frame if ID variables are unique or the input data.frame with a new .id column.

Examples

Run this code
mydf <- data.frame(IDA = c("a", "a", "a", "b", "b"),
                   IDB = c(1, 1, 1, 1, 1), values = 1:5)
mydf
getanID(mydf, c("IDA", "IDB"))

mydf <- data.frame(IDA = c("a", "a", "a", "b", "b"),
                   IDB = c(1, 2, 1, 1, 2), values = 1:5)
mydf
getanID(mydf, 1:2)

rm(mydf)

Run the code above in your browser using DataLab