【问题标题】:How to generate AA,AB,AC...,BA,BB,BC...,CA,CB,CC,...,ZZ series in R?如何在 R 中生成 AA,AB,AC...,BA,BB,BC...,CA,CB,CC,...,ZZ 系列?
【发布时间】:2017-11-21 07:12:18
【问题描述】:

我必须生成一个字母系列(AA,AB,AC...,BA,BB,BC...,CA,CB,CC,...,ZZ),类似于 Excel 的默认列名文件。

我用下面的组合函数试了一下,

combn(LETTERS,2)

但是,这不符合我的要求。

【问题讨论】:

  • 试试do.call(paste0, expand.grid(rep(list(LETTERS), 2))[2:1]) 有了这个你也可以为n =3生成,即do.call(paste0, expand.grid(rep(list(LETTERS), 3))[3:1])及以上`
  • @akrun 非常感谢。这是我要找的那个:)

标签: r combinations


【解决方案1】:
as.vector(sapply(LETTERS,function(x) sapply(LETTERS , function(y) paste0(x,y))))

【讨论】:

  • 不错的答案。检查this link 的格式。
【解决方案2】:

另一种选择是outer

out <- c(outer(LETTERS, LETTERS, paste0))
head(sort(out))
# [1] "AA" "AB" "AC" "AD" "AE" "AF"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2019-06-07
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多