creating random numbers with the function seed

Hi, this is my first time here. I basically use SAS but since my company has decided to switch to R. I need to learn this fast. I have a question on how to generate a set of random numbers using the function seed.

I have a list of Patient ID's which I wish to substitute with a set of random numbers but still be able to trace this back in future. In SAS this works good with the seed function. I have watched a lot of youtube videos on how to create random numbers with the function seed in R but how do I apply this to a list in a table (I think in R its called frames). I have a column patient_id in the table, I need a new column with lets say patient_id_new with the new set of random id's. I will be grateful for any help

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

Thank you very much nirgrahamuk. Let me try that and see

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.