问题
Assume I have 4 vectors of character
elements:
s1 <- c("o", "ó")
s2 <- c("c", "ć")
s3 <- c("o", "ó")
s4 <- c("z", "ź", "ż")
I want to build 4-element vectors that are all possible combinations of elements from s1
, s2
, s3
, s4
in a way that in one of a result vectors 1-st, -2nd, 3-rd and 4-th element comes from s1
, s2
, s3
, s4
, respectively.
For example, I would like to get the following result vectors:
[1] "o", "c", "o", "z"
[1] "ó", "c", "o", "z"
[1] "o", "ć", "o", "z"
...
[My general goal of it is to produce all possible words from a given word in my native, assuming the given word have some diacritic marks ommited. I firstly extract all character candidates for being a diacritic mark and then want to inject them or their diacritic equivalents.]
回答1:
It's not super elegant but using the definitions for s1
-s4
above, here is a quick for loop of for loops that will print the vectors you requested.
for(a in s1){
for(b in s2){
for(c in s3){
for(d in s4){
print(c(a,b,c,d))
}
}
}
}
Output:
[1] "o" "c" "o" "z"
[1] "o" "c" "o" "z"
[1] "o" "c" "o" "z"
[1] "o" "c" "ó" "z"
[1] "o" "c" "ó" "z"
[1] "o" "c" "ó" "z"
[1] "o" "c" "o" "z"
[...]
As shadow said above, expand.grid()
is a much more elegant solution:
expand.grid(list(s1,s2,s3,s4))
Output:
Var1 Var2 Var3 Var4
1 o c o z
2 ó c o z
3 o c o z
4 ó c o z
5 o c ó z
6 ó c ó z
7 o c ó z
8 ó c ó z
[...]
回答2:
You may also check data.table
library(data.table)
res <- CJ(s1=s1,s2=s2,s3=s3,s4=s4) #order the result by columns
Checking with results obtained from expand.grid
res2 <- expand.grid(s1=s1,s2=s2,s3=s3,s4=s4,stringsAsFactors=F)
res2New <- res2[with(res2, order(s1,s2,s3,s4)),]
all.equal(res2New, as.data.frame(res),check.attributes=F)
#[1] TRUE
来源:https://stackoverflow.com/questions/24506602/get-all-possible-string-sequences-where-each-element-comes-from-different-set-in