primKey: Primary Key

Description Usage Arguments Value Examples

View source: R/primKey.R

Description

Look for "primary keys" of a data frame. The primary key is a variable (or combination of variables) that uniquely identify all rows.

Usage

1
primKey(df, varz = 1:dim(df)[2])

Arguments

df

A data frame in which to find the primary key.

varz

A numeric or character vector representing the candidate columns or variable names to consider for membership in the primary key, default is to include all variables, 1:dim(df)[2].

Value

A list with two elements, npk an integer giving the number of variables that make up the primary key, and k a list of character vectors) identifying the variable names that make up the primary key.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
mydat1 <- data.frame(
 group=c("a", "a", "a", "b", "b", "c", "c", "d", "d"),
 id=1:9,
 value=rnorm(9))
mydat2 <- data.frame(
 group=c("a", "a", "a", "b", "b", "c", "c", "d", "d"),
 id=c(1:3, 1:2, 1:2, 1:2),
 value=rnorm(9))
mydat3 <- data.frame(
 group=c("a", "a", "a", "b", "b", "c", "c", "d", "d"),
 name=c("ax", "ax", "ax", "be", "be", "cat", "cat", "dog", "dog"),
 id=c(1:3, 1:2, 1:2, 1:2),
 value=rnorm(9))
# primary key made up of one variable
primKey(mydat1[, c("group", "id")])
# primary key made up of two variables
primKey(mydat2[, c("group", "id")])
# two primary keys, each made up of two variables
primKey(mydat3[, c("group", "name", "id")])

JVAdams/jvamisc documentation built on Aug. 11, 2021, 6:43 a.m.