我對 R 比較陌生,我已經為我的問題尋找了答案,但沒有運氣。希望你能幫助我。
到目前為止,這是我撰寫的代碼:
#load library
library(gtools)
#the Options
my_list <- c("come_in-blatt-1",
"come_in-blatt-2",
"come_in-front-1",
"come_in-front-2"
)
#get all permutations
result <- permutations(n = 4 , r = 11, v = my_list,repeats.allowed=T)
#save as:
write.csv(result, file = "result-test3.csv")
#open as CSV file
command <- paste("open excel", file = "result-test3.csv")
system(command)
我想看到所有可能的排列/組合,但有一些限制,這就是我迷失的地方。我需要在 V1 和 V2 中只有“come_in-blatt-1”和“come_in-blatt-2”。對于所有其他 V3、V4 到 V11(在這種情況下 -> r = 11),將只是“come_in-front-1”和“come_in-front-2”。
有沒有辦法讓R向我展示這個?到目前為止,我已經打開 Excel 并過濾掉了我不需要的內容,但是現在我遇到的問題是串列比 excel 可以使用的要大。:-/
謝謝。
uj5u.com熱心網友回復:
如果我了解 blatt 選項可以在V1或 中,這是一種方法V2。
#load library
library(gtools)
## Create V1:V2 perms separately from V3:V11
blatts = permutations(n = 2, r = 2, v = c("come_in-blatt-1", "come_in-blatt-2"), repeats.allowed = TRUE)
fronts = permutations(n = 2, r = 9, v = c("come_in-front-1", "come_in-front-2"), repeats.allowed = TRUE)
## Combine the two
## For each row of blatts, repeat the matrix and combine with fronts
res = lapply(seq_len(nrow(blatts)),
function(i){
cbind(blatts[rep(i, nrow(fronts)), ],
fronts)
})
## combine the list into one
res = do.call('rbind', res)
## look at the dim of the resulting matrix
dim(res)
#> [1] 2048 11
## verify the work
all(res[, 1:2] %in% c("come_in-blatt-1", "come_in-blatt-2"))
#> [1] TRUE
all(res[, 3:11] %in% c("come_in-front-1", "come_in-front-2"))
#> [1] TRUE
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351653.html
下一篇:在R中查找XPath節點的名稱
