Random alphanumeric strings

Hello,

I hope you are very well.
Thanks a lot!, here it is some simulation code:

library(tidyverse)

data.frame(i= c(sample(1:30000)),
P = c(sample(LETTERS[1:21], 10, replace = T)),
D = as.Date("31/05/2021", format="%d/%m/%Y"),
e = c(sample(c("a","b","c"),size=100,replace = T,prob=c(0.6,0.3,0.1))),
C = c(sample(LETTERS[1:21], 10, replace = T)),
k= c(sample(1:120)),
F =c(sample(1:5)),
o = c(sample(1:30000)),
t = c(sample(1:30000)),
t = c(sample(1:30000)),
L = c(sample(LETTERS[1:21], 10, replace = T))) %>%
mutate(s = paste(o, t, t, sep = ""))

How can I create a random alphanumeric ID in the "s" variable?
Some options?
Thanks!

Maybe use stringi::stri_rand_strings():

library(tidyverse)
library(stringi)

data.frame(i= c(sample(1:30000)),
           P = c(sample(LETTERS[1:21], 10, replace = T)),
           D = as.Date("31/05/2021", format="%d/%m/%Y"),
           e = c(sample(c("a","b","c"),size=100,replace = T,prob=c(0.6,0.3,0.1))),
           C = c(sample(LETTERS[1:21], 10, replace = T)),
           k= c(sample(1:120)),
           F =c(sample(1:5)),
           o = c(sample(1:30000)),
           t = c(sample(1:30000)),
           t = c(sample(1:30000)),
           L = c(sample(LETTERS[1:21], 10, replace = T))) %>%
  rowwise() %>% 
  mutate(s = paste0(stri_rand_strings(1, 5, "[A-Z]"), stri_rand_strings(1, 4, "[0-9]"), stri_rand_strings(1, 1, "[A-Z]"))) %>% 
  ungroup()

# A tibble: 30,000 x 12
       i P     D          e     C         k     F     o     t   t.1 L     s         
   <int> <chr> <date>     <chr> <chr> <int> <int> <int> <int> <int> <chr> <chr>     
 1 21398 D     2021-05-31 a     Q        36     2  4530  4116 18621 A     DMUOL2472R
 2 19557 O     2021-05-31 b     Q        84     1 17660 24714 26124 S     XTFJS1702S
 3 16566 S     2021-05-31 a     G        99     3 15562 24113  7155 L     MVACP8644V
 4 18974 Q     2021-05-31 a     G       101     4  5364 27218 12805 M     HZGBN5767G
 5 15366 P     2021-05-31 a     Q        54     5  7615 27503 23493 A     GFAYD1066I
 6  2257 U     2021-05-31 b     C       115     2 26735 29621 11397 Q     CLYLG8054S
 7 29447 R     2021-05-31 b     G        93     1  9657  2990  8793 H     RTFIH2830Z
 8  8376 D     2021-05-31 a     L        74     3 28361 22925  4644 K     PQZQW2437J
 9 25292 K     2021-05-31 b     D        10     4 13053 19331 21739 J     RHMEA6043V
10 21959 L     2021-05-31 b     Q        23     5 29990  4514 27470 C     MYAKI9349Y
# ... with 29,990 more rows
# i Use `print(n = ...)` to see more rows



You gave me a SOLUTION
THANKS!!!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.