R中的特定排序
我正在为一个特定的输入制作所有可能的组合,但它也必须根据输入的顺序进行排序。由于这些组合的大小不同,我很难找到之前发布的答案 我想知道这是否可行 输入:R中的特定排序,r,sorting,R,Sorting,我正在为一个特定的输入制作所有可能的组合,但它也必须根据输入的顺序进行排序。由于这些组合的大小不同,我很难找到之前发布的答案 我想知道这是否可行 输入: D N A 3 这意味着我需要以最多3个字符串的所有组合输出它: D DD DDD DDN DDA DND DNA . . 基本上是升序,如果我们考虑 d> p>一个解决方案,使用我刚才发现的一个随机库(我可能使用错了)被称为 ITEPC。p> 生成所有的组合,对元素进行因子分析,排序,然后生成字符串 ordered_combn = fu
D N A 3
这意味着我需要以最多3个字符串的所有组合输出它:
D
DD
DDD
DDN
DDA
DND
DNA
.
.
基本上是升序,如果我们考虑<代码> d> p>一个解决方案,使用我刚才发现的一个随机库(我可能使用错了)被称为<代码> ITEPC。p> 生成所有的组合,对元素进行因子分析,排序,然后生成字符串
ordered_combn = function(elems) {
require(data.table)
require(iterpc)
I = lapply(seq_along(elems), function(i) iterpc::iterpc(table(elems), i, replace=TRUE, ordered=TRUE))
I = lapply(I, iterpc::getall)
I = lapply(I, as.data.table)
dt = rbindlist(I, fill = TRUE)
dt[is.na(dt)] = ""
cols = paste0("V", 1:length(elems))
dt[, (cols) := lapply(.SD, factor, levels = c("", elems)), .SDcols = cols]
setkey(dt)
dt[, ID := 1:.N]
dt[, (cols) := lapply(.SD, as.character), .SDcols = cols]
dt[, ord := paste0(.SD, collapse = ""), ID, .SDcols = cols]
# return dt[, ord] as an ordered factor for neatness
dt
}
elems = c("D", "N", "A")
combs = ordered_combn(elems)
combs
输出
V1 V2 V3 ID ord
1: D 1 D
2: D D 2 DD
3: D D D 3 DDD
4: D D N 4 DDN
5: D D A 5 DDA
6: D N 6 DN
7: D N D 7 DND
8: D N N 8 DNN
...
这里有一个可能的解决方案:
generateCombs <- function(x, n){
if (n == 1) return(x[1]) # Base case
# Create a grid with all possible permutations of 0:n. 0 == "", and 1:n correspond to elements of x
permutations = expand.grid(replicate(n, 0:n, simplify = F))
# Order permutations
orderedPermutations = permutations[do.call(order, as.list(permutations)),]
# Map permutations now such that 0 == "", and 1:n correspond to elements of x
mappedPermutations = sapply(orderedPermutations, function(y) c("", x)[y + 1])
# Collapse each row into a single string
collapsedPermutations = apply(mappedPermutations, 1, function(x) paste0(x, collapse = ""))
# Due to the 0's, there will be duplicates. We remove the duplicates in reverse order
collapsedPermutations = rev(unique(rev(collapsedPermutations)))[-1] # -1 removes blank
# Return as data frame
return (as.data.frame(collapsedPermutations))
}
x = c("D", "N", "A")
n = 3
generateCombs(x, n)
订购的原因是什么?我不懂DND,这不是DDN吗?你可以对整数进行排序,然后对字符进行细分。
chartr('123','DNA',c('1','11','123','122','133'))
@rawr这是升序,DDN在DDDYeah之后。这给了我一个想法,我可能需要做一个矩阵,其中每个字符都由一个特征表示(转换为数字),然后按特征排序,因为你没有对每个字符串中的字母进行排序。。
generateCombs <- function(x, n){
if (n == 1) return(x[1]) # Base case
# Create a grid with all possible permutations of 0:n. 0 == "", and 1:n correspond to elements of x
permutations = expand.grid(replicate(n, 0:n, simplify = F))
# Order permutations
orderedPermutations = permutations[do.call(order, as.list(permutations)),]
# Map permutations now such that 0 == "", and 1:n correspond to elements of x
mappedPermutations = sapply(orderedPermutations, function(y) c("", x)[y + 1])
# Collapse each row into a single string
collapsedPermutations = apply(mappedPermutations, 1, function(x) paste0(x, collapse = ""))
# Due to the 0's, there will be duplicates. We remove the duplicates in reverse order
collapsedPermutations = rev(unique(rev(collapsedPermutations)))[-1] # -1 removes blank
# Return as data frame
return (as.data.frame(collapsedPermutations))
}
x = c("D", "N", "A")
n = 3
generateCombs(x, n)
collapsedPermutations
1 D
2 DD
3 DDD
4 DDN
5 DDA
6 DN
7 DND
8 DNN
9 DNA
10 DA
11 DAD
...