creating random numbers with the function seed

Something along these lines

#use R's sleep data with ID column as basis for example
(my_data <- sleep)

#get out all the unique ids

(unique_ids <- unique(my_data$ID))

(num_of_unique_ids <- length(unique_ids))
#for each unique id assign a random integer
# set seed for reproducibility
set.seed(42)
(rand_ids<-sample.int(n = 10^6, #many possible integers
           size=num_of_unique_ids,
           replace=FALSE))

(ids_lookups <- data.frame(ID=unique_ids,
                           new_ID=rand_ids))

library(tidyverse) # or sub library - library(dplyr) which contains the left_join function

(my_data_with_new_ids <- left_join(my_data,
                                  ids_lookups))
1 Like