jeudi 25 juin 2020

Randomly Assigning ID Tags to different Sections in R

I am having trouble randomly assigning ID Tags to a Section. I generated a random string of 347 made up three-letter ID Tags (i.e., sbn, hjk, cvl). I would like each individual ID Tag to be randomly assigned to a Section (i.e., w,v,g,b). There are a total of 47 Sections. After assigning the ID Tags to one Section, I would very much like to create a data frame. Below is what I have so far:

##################### Create the ID Tags

  random.tags <- function(n=347, length=3)
  {
  randomString <- c(1:n)                  
 for (i in 1:n)
  {
  randomString[i] <- paste(sample(c(letters, letters),
                                length, replace=TRUE),
                         collapse="")

} return(randomString) }

 Tags<-random.tags()

################################# Create the Sections

 Sections<-c("w","v","u","t","s","r","q","p","o","n","m","l","k","j","i","g","f","e","d","c","b","a",
             "aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm",
             "nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy")

I thought using the sample function in R would allow me to randomly assign the ID Tags to one Section but I am not able to because the rows are different lengths.

  df <- data.frame(Tag=sample(Tags, replace=TRUE),
             Section=sample(Sections, replace=TRUE))  # doesn't work
                                                                               

Thank you very much in advance for your help.




Aucun commentaire:

Enregistrer un commentaire