从向量r创建所有可能的字符串

发布于 2025-01-25 19:53:09 字数 176 浏览 1 评论 0原文

我有一个带四个字母的矢量。

letter <- c('x', 'y', 'z', 'j')

我想创建所有由20个字母组成的字符串,重复我的四个字母。

我尝试使用combn()随机软件包,但我失败了。

I have a vector with four letters.

letter <- c('x', 'y', 'z', 'j')

I would like to create all possible strings composed of 20 letters, repeating my four ones.

I tried with combn() and the random package but I failed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢好甜 2025-02-01 19:53:09

首先,您应该意识到,您的计算机可能无法握住巨大的字符串(所有20个字母的组合),例如,

> rep(strrep("a", 20), 4^20)
Error: cannot allocate vector of size 8192.0 Gb

如果我们的字符串较短,似乎仍然可以这样做。


以下是几个基本R选项,可以产生所有组合

f1 <- function(n) {
    if (n == 1) {
        return(letter)
    }
    v <- Recall(n - 1)
    unlist(lapply(letter, paste0, v))
}

f2 <- function(n) {
    v <- letter
    for (k in 2:n) {
        v <- unlist(lapply(letter, paste0, v))
    }
    v
}

f3 <- function(n) {
    lst <- rep(list(letter), n)
    Reduce(function(x, y) do.call(paste0, expand.grid(x, y)), lst)
}


f4 <- function(n) {
    do.call(paste0, expand.grid(rep(list(letter), n)))
}

,而速度基准为

> n <- 10

> microbenchmark(
+     f1(n),
+     f2(n),
+     f3(n),
+     f4(n),
+     times = 10L
+ )
Unit: milliseconds
  expr       min        lq      mean    median        uq       max neval
 f1(n)  434.0156  592.1137  650.2692  617.9684  688.4830  903.9767    10
 f2(n)  461.7505  604.7973  749.9571  725.1946  969.3452 1097.1925    10
 f3(n) 1086.2451 1458.1349 1615.1434 1585.8263 1661.5754 2105.3784    10
 f4(n) 1780.0759 1926.0896 2192.6231 2256.5225 2288.2689 2703.9856    10

First of all, you should be aware that your computer may not able to hold that huge amont of strings (all combinations of 20 letters), e.g.,

> rep(strrep("a", 20), 4^20)
Error: cannot allocate vector of size 8192.0 Gb

However, if we have shorter strings, it seems still possible to do that.


Here are a couple of base R options that could produce all combinations

f1 <- function(n) {
    if (n == 1) {
        return(letter)
    }
    v <- Recall(n - 1)
    unlist(lapply(letter, paste0, v))
}

f2 <- function(n) {
    v <- letter
    for (k in 2:n) {
        v <- unlist(lapply(letter, paste0, v))
    }
    v
}

f3 <- function(n) {
    lst <- rep(list(letter), n)
    Reduce(function(x, y) do.call(paste0, expand.grid(x, y)), lst)
}


f4 <- function(n) {
    do.call(paste0, expand.grid(rep(list(letter), n)))
}

and the speed benchmarking is

> n <- 10

> microbenchmark(
+     f1(n),
+     f2(n),
+     f3(n),
+     f4(n),
+     times = 10L
+ )
Unit: milliseconds
  expr       min        lq      mean    median        uq       max neval
 f1(n)  434.0156  592.1137  650.2692  617.9684  688.4830  903.9767    10
 f2(n)  461.7505  604.7973  749.9571  725.1946  969.3452 1097.1925    10
 f3(n) 1086.2451 1458.1349 1615.1434 1585.8263 1661.5754 2105.3784    10
 f4(n) 1780.0759 1926.0896 2192.6231 2256.5225 2288.2689 2703.9856    10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文