Skip to content

Commit

Permalink
Use vectorized randomNames() call
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo authored and joshwlambert committed Oct 23, 2023
1 parent 6ee8e76 commit 5ec9253
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions R/add_cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,30 +189,14 @@ NULL
#' @name .add_info
.add_names <- function(.data) {
# create sample of names so there are no duplicates
cases_m <- sum(.data$gender == "m")
cases_f <- sum(.data$gender == "f")
names <- list(
names_masc = randomNames::randomNames(
n = cases_m,
which.names = "both",
name.sep = " ",
name.order = "first.last",
gender = 0,
sample.with.replacement = FALSE
),
names_fem = randomNames::randomNames(
n = cases_f,
which.names = "both",
name.sep = " ",
name.order = "first.last",
gender = 1,
sample.with.replacement = FALSE
)
.data$case_name <- randomNames::randomNames(
which.names = "both",
name.sep = " ",
name.order = "first.last",
gender = .data$gender,
sample.with.replacement = FALSE
)

.data$case_name[.data$gender == "m"] <- names$names_masc # move to start of df
.data$case_name[.data$gender == "f"] <- names$names_fem

# left join corresponding names to infectors preserving column and row order
infector_names <- data.frame(id = .data$id, infector_name = .data$case_name)
col_order <- c(colnames(.data), "infector_name")
Expand Down

0 comments on commit 5ec9253

Please sign in to comment.