在r中创建所有可能的二元变量组合

时间:2014-06-01 08:55:48

标签: r combinations

我有这个数据集

install.packages("combinat")
install.packages("quantmod")
library(quantmod)
library(combinat)
getSymbols("AAPL",from="2013-01-01")
data<-AAPL
p1<-4  
dO<-data[,1]
dC<-data[,4]
emaO<-EMA(dO,n=p1)
emaC<-EMA(dC,n=p1)

和这个变量

Pos_emaO_dO_UP<-emaO>dO
Pos_emaO_dO_D<-emaO<dO
Pos_emaC_dC_UP<-emaC>dC
Pos_emaC_dC_D<-emaC<dC
Pos_emaC_dO_D<-emaC<dO
Pos_emaC_dO_UP<-emaC>dO
Pos_emaO_dC_UP<-emaO>dC
Pos_emaO_dC_D<-emaO<dC

或者

Pos_emaO_dO<-ifelse(emaO>dO,"UP",
                    ifelse(emaO<dO,"D","Equal"))
Pos_emaC_dC<-ifelse(emaC>dC,"UP",
                    ifelse(emaC<dC,"D","Equal"))
Pos_emaC_dO<-ifelse(emaC>dO,"UP",
                    ifelse(emaC<dO,"D","Equal"))
Pos_emaO_dC<-ifelse(emaO>dC,"UP",
                    ifelse(emaO<dC,"D","Equal"))



  frame<-data.frame(Pos_emaO_dO_UP,Pos_emaO_dO_D,Pos_emaC_dC_UP,Pos_emaC_dC_D,Pos_emaC_dO_D,Pos_emaC_dO_UP,Pos_emaO_dC_UP,Pos_emaO_dC_D)
    vector<-colnames(frame)<-c("Pos_emaO_dO_UP","Pos_emaO_dO_D","Pos_emaC_dC_UP","Pos_emaC_dC_D","Pos_emaC_dO_D","Pos_emaC_dO_UP","Pos_emaO_dC_UP","Pos_emaO_dC_D")
Three<-as.data.frame(combn(vector,3))
Two<-as.data.frame(combn(vector,2))
Four<-as.data.frame(combn(vector,4))

EDITED: 我把这个问题编辑了。我需要删除这些组合(V1:Vn),例如,它们具有两种可能的变量组合。 e.x. Pos_emaO_dO_UP&amp; Pos_emaO_dO_D。通常,如果D或UP之前的公式相同并且(UP&amp; D)都在其中,则删除此组合。我尝试了一些独特的,但没有成功。

注意:我希望然后使用像粘贴一样的粘贴(组合,折叠=&#34;&amp;&#34;)并使用这个&#34;粘贴&#34;组合将主表子集化,然后像子集(main_table,&#34; pasted_combination&#34;)并运行我所拥有的槽式lapply函数。它会起作用吗? 感谢

1 个答案:

答案 0 :(得分:0)

我还不太确定我是否理解你,但请考虑这个例子。我们有3个变量(x1x2x3),每个变量可以是UpDEqual。要计算3个变量的所有可能组合,您可以使用expand.grid

dd <- expand.grid(x1 = c("UP", "D", "Equal"), 
                  x2 = c("UP", "D", "Equal"), 
                  x3 = c("UP", "D", "Equal"))

dd
#      x1    x2    x3
#1     UP    UP    UP
#2      D    UP    UP
#3  Equal    UP    UP
#4     UP     D    UP
#5      D     D    UP
#6  Equal     D    UP
#7     UP Equal    UP
#8      D Equal    UP
#9  Equal Equal    UP
#10    UP    UP     D
#11     D    UP     D
#12 Equal    UP     D
#13    UP     D     D
#14     D     D     D
#15 Equal     D     D
#16    UP Equal     D
#17     D Equal     D
#18 Equal Equal     D
#19    UP    UP Equal
#20     D    UP Equal
#21 Equal    UP Equal
#22    UP     D Equal
#23     D     D Equal
#24 Equal     D Equal
#25    UP Equal Equal 
#26     D Equal Equal
#27 Equal Equal Equal

现在据我了解,您只需要UPD不是两者的行。为此,您可以使用apply检查每一行UPD的外观,然后选择正确的行。

#create a new column "check" and set 1 if "UP" AND "D" are present in row
dd$check <- apply(dd, 1, function(x) ifelse("UP" %in% x & "D" %in% x, 1 , 0))

结果是:

> dd
#      x1    x2    x3 check
#1     UP    UP    UP     0
#2      D    UP    UP     1
#3  Equal    UP    UP     0
#4     UP     D    UP     1
#5      D     D    UP     1
#6  Equal     D    UP     1
#7     UP Equal    UP     0
#8      D Equal    UP     1
#9  Equal Equal    UP     0
#10    UP    UP     D     1
#11     D    UP     D     1
#12 Equal    UP     D     1
#13    UP     D     D     1
#14     D     D     D     0
#15 Equal     D     D     0
#16    UP Equal     D     1
#17     D Equal     D     0
#18 Equal Equal     D     0
#19    UP    UP Equal     0
#20     D    UP Equal     1
#21 Equal    UP Equal     0
#22    UP     D Equal     1
#23     D     D Equal     0
#24 Equal     D Equal     0
#25    UP Equal Equal     0
#26     D Equal Equal     0
#27 Equal Equal Equal     0

现在您可以选择check为0的所有行。

dd <- dd[dd$check == 0,]
dd
#      x1    x2    x3   check
#1     UP    UP    UP       0
#3  Equal    UP    UP       0
#7     UP Equal    UP       0
#9  Equal Equal    UP       0
#14     D     D     D       0
#15 Equal     D     D       0
#17     D Equal     D       0      
#18 Equal Equal     D       0      
#19    UP    UP Equal       0      
#21 Equal    UP Equal       0      
#23     D     D Equal       0      
#24 Equal     D Equal       0      
#25    UP Equal Equal       0      
#26     D Equal Equal       0      
#27 Equal Equal Equal       0      

当然,您可以删除额外的列(已在先前的子集中)或现在:

dd <- dd[, -4]

那是你所追求的吗?