R.字母组合

use*_*545 3 combinations r

我应该有这样的输出:

AAAA
AAAG
AAAC
AAAT
AAGA
AAGG
...
Run Code Online (Sandbox Code Playgroud)

我想先用数字做这个,将"A"表示为1,将"G"表示为2,等等......

1111
1112
...
Run Code Online (Sandbox Code Playgroud)

然后将1转换为"A",依此类推.我找到了这个函数expand.grid,但这给了我一个包含4个变量(4列)的数据框,每个变量都有一个数字.

你还有其他想法吗?

提前致谢.

Mat*_*rde 9

编辑:我的原始答案错误地认为你已经有了索引的向量.要从头开始生成这些字母的所有可能组合的向量,请尝试以下方法:

x <- expand.grid(rep(list(c('A', 'G', 'T', 'C')), 4))
do.call(paste0, x)
Run Code Online (Sandbox Code Playgroud)

你可以这样做chartr.

x <- c(1111, 1112, 1113, 1114, 1121)
chartr('1234', 'AGCT', x)
# [1] "AAAA" "AAAG" "AAAC" "AAAT" "AAGA"
Run Code Online (Sandbox Code Playgroud)

  • 如果你仍然想要使用`chartr`(即使它不是最好的解决方案),你可以分两步完成:`step1 = expand.grid(l1 = 1:4,l2 = 1:4,l3 = 1:4,l4 = 1:4)`和`result = chartr('1234','AGCT',paste0(step1 [,1],step1 [,2],step1 [,3],step1 [,4] ))` (2认同)