Learn R Programming

privacyR (version 1.0.1)

anonymize_dates: Anonymize Dates

Description

Anonymizes dates by shifting them by a random offset or rounding to a specified granularity. Shifting preserves relative time differences.

Usage

anonymize_dates(
  x,
  method = c("shift", "round"),
  days_shift = NULL,
  granularity = "month",
  seed = NULL
)

Value

A Date vector of anonymized dates (or character vector for "month_year" granularity)

Arguments

x

A vector of dates (Date, POSIXct, or character that can be coerced to Date)

method

Character string specifying anonymization method: "shift" (default) shifts all dates by a random offset, "round" rounds dates to specified granularity

days_shift

For "shift" method: number of days to shift (default: random between -365 and 365)

granularity

For "round" method: "day", "week", "month", "month_year", "quarter", or "year" (default: "month"). "month_year" returns character strings in "YYYYMM" format (e.g., "202005" for May 2020).

seed

An optional seed for reproducible anonymization

Examples

Run this code
dates <- as.Date(c("2020-01-15", "2020-03-20", "2020-06-10"))
anonymize_dates(dates, method = "shift", seed = 123)
anonymize_dates(dates, method = "round", granularity = "month")
anonymize_dates(dates, method = "round", granularity = "month_year")

Run the code above in your browser using DataLab