在R条件下生产的组合

在R条件下生产的组合,r,combinations,R,Combinations,嗨,我有4个字符串来执行组合-1)PES 2)PEA 3)PAL 4)PSL 我必须编写一个脚本来执行组合(组合-ncr公式),这样我只需要与以下算法组合 If I have PAL in the combination output then keep PAL only and remove other string (PEA,PES,PSL) for egs if I have one of combination as PAL PSL then I only need PAL in out

嗨,我有4个字符串来执行组合-1)PES 2)PEA 3)PAL 4)PSL

我必须编写一个脚本来执行组合(组合-ncr公式),这样我只需要与以下算法组合

If I have PAL in the combination output then keep PAL only and remove other string (PEA,PES,PSL) for egs if I have one of combination as PAL PSL then I only need PAL in output which I think will be in data frame format.
如果我的输出中有PEA而不是PAL,那么只保留PEA而不包括其他(PES和PSL),例如,如果我有类似PEA PSL的组合,那么我的输出中只需要PEA

都是弦


结果-1)PAL 2)PEA 3)PSL+PES

这将返回大小为4到1的组合,并从任何包含PAL的项目中删除非PAL项目。它使用
combn
函数返回特定大小的组合集:

lapply( lapply(1:4 , combn, x=strings), # modify if smaller set of sizes needed
  function(cx){
  apply(cx, 2, function(col) if( any(col=="PAL") ) {"PAL"} else { paste( col ) } ) } )
不清楚您希望如何交付这些数据,因为在“数据帧”中返回它们实际上没有意义,因为它们的长度不同

[[1]]
[1] "PES" "PEA" "PAL" "PSL"

[[2]]
[[2]][[1]]
[1] "PES" "PEA"

[[2]][[2]]
[1] "PAL"

[[2]][[3]]
[1] "PES" "PSL"

[[2]][[4]]
[1] "PAL"

[[2]][[5]]
[1] "PEA" "PSL"

[[2]][[6]]
[1] "PAL"


[[3]]
[[3]][[1]]
[1] "PAL"

[[3]][[2]]
[1] "PES" "PEA" "PSL"

[[3]][[3]]
[1] "PAL"

[[3]][[4]]
[1] "PAL"


[[4]]
[1] "PAL"