add_intid: Add an grouped integer ID to a dataframe

Description Usage Arguments Examples

View source: R/data_manipulation.R

Description

Adds a new unique ID to each member of a group in a dataframe. This is especially useful when processing data for upload to a database that uses integer IDs as unique primary keys. A single unique ID can be generated for each member of a group without first having to separate out the group and filtering to unique values.

Usage

1
add_intid(dat, key_cols, id_name, start_id)

Arguments

dat

A dataframe

key_cols

The group of variables that will be assigned a unique ID

id_name

The name to assign to the ID variable

start_id

The ID value used to start the ID sequence. If 0 the IDs start at 1.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Create an example dataframe
dat = dplyr::data_frame(lat = c(rep(47.6590, 7),
                         rep(47.6348, 3),
                         47.9033),
                 lon = c(rep(-122.4657, 4),
                         rep(-122.8743, 3),
                         rep(-122.8876, 2),
                         -122.8345, -122.4443),
                 species = c("chum", "chin", "chin",
                             "sthd", "chin", "sthd",
                             "sthd", "chum", "chum",
                             "pink", "pink"))

# Add an integer ID, grouped by the key columns
dat = add_intid(dat, key_cols = c("lat", "lon", "species"),
                id_name = "survey_id", start_id = 100)

arestrom/remisc documentation built on Oct. 7, 2017, 4:57 p.m.