split(inputdata, substr(inputdata,1,1))
# $A
# [1] "A1" "A2" "A3"
# $B
# [1] "B1" "B2" "B3"
# $C
# [1] "C1" "C2" "C3"
如果我们打电话
expand.grid
在这方面,我们可以,它会给我们每个组合
A
,
B
,和
C
:
head( do.call(expand.grid, split(x, substr(x,1,1))) )
# A B C
# 1 A1 B1 C1
# 2 A2 B1 C1
# 3 A3 B1 C1
# 4 A1 B2 C1
# 5 A2 B2 C1
# 6 A3 B2 C1
现在我们可以
paste(..., collapse="+")
这是:
apply(do.call(expand.grid, split(x, substr(x,1,1))), 1, paste, collapse="+")
# [1] "A1+B1+C1" "A2+B1+C1" "A3+B1+C1" "A1+B2+C1" "A2+B2+C1" "A3+B2+C1"
# [7] "A1+B3+C1" "A2+B3+C1" "A3+B3+C1" "A1+B1+C2" "A2+B1+C2" "A3+B1+C2"
# [13] "A1+B2+C2" "A2+B2+C2" "A3+B2+C2" "A1+B3+C2" "A2+B3+C2" "A3+B3+C2"
# [19] "A1+B1+C3" "A2+B1+C3" "A3+B1+C3" "A1+B2+C3" "A2+B2+C3" "A3+B2+C3"
# [25] "A1+B3+C3" "A2+B3+C3" "A3+B3+C3"
-
如果你的数据很大
展开.grid
可能会耗尽内存;
-
这是我认为
apply
在
data.frame
是安全合理的,因为我们知道它的所有输入都是同一类的