Subset Must Be Logical Error in R
I Use This Code and Try to Subset It. the Source Data Are Below Library(Quantmod) Library(Combinat) getSymbols("AAPL", From="2012-01-01") Data
I use this code and try to subset it. The source data are below
library(quantmod)
library(combinat)
getSymbols("AAPL",from="2012-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
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)
colnames(frame)<-vector<-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")
This is the code was used to take out the nonsense columns
comb<-as.data.frame(combn(vector,4))
comb
rc<-comb[!sapply(comb, function(x) any(duplicated(sub('_D|_UP', '', x))))]
And then I've tried to subset the main frame by combination of variables from first column
kb<-as.vector(rc[,1])
vr<-paste(kb,collapse=" & ")
subset(frame,vr)
But the result is : subset must be logical. I need to subset the frame by all these combinations like
subset(frame,Pos_emaO_dO_D & Pos_emaC_dC_UP & Pos_emaC_dO_D & Pos_emaO_dC_UP) #etc.
one by one a then use on each subset function which I wrote to analyse
subsetit<-function(x,y){
f<-head(subset(main_table,y & x),-1)
1d_l_prof<-nrow(f[f$Profit_L_1>0.1,])/nrow(f)
2d_l_prof<-(nrow(f[f$Profit_L_2>0.1,])/nrow(f))/2
3d_l_prof<-(nrow(f[f$Profit_L_3>0.1,])/nrow(f))/3
4d_l_prof<-(nrow(f[f$Profit_L_4>0.1,])/nrow(f))/4
5d_l_prof<-(nrow(f[f$Profit_L_5>0.1,])/nrow(f))/5
6d_l_prof<-(nrow(f[f$Profit_L_6>0.1,])/nrow(f))/6
7d_l_prof<-(nrow(f[f$Profit_L_7>0.1,])/nrow(f))/7
8d_l_prof<-(nrow(f[f$Profit_L_8>0.1,])/nrow(f))/8
9d_l_prof<-(nrow(f[f$Profit_L_9>0.1,])/nrow(f))/9
10d_l_prof<-(nrow(f[f$Profit_L_10>0.1,])/nrow(f))/10
df<-data.frame(1d_l_prof,2d_l_prof,3d_l_prof,4d_l_prof,5d_l_prof,6d_l_prof,7d_l_prof,8d_l_prof,9d_l_prof,10d_l_prof)
return(df)
}
Where should be the problem of subset error? Will works some
lapply(rc[,c(1:length(rc))],subsetit] ?
1 Answer
Posting comment as an answer since Bury says it works:
It appears your vr is a character string. If you go this route, you'll need to do the infamous eval(parse(text= paste0('subset(frame,' , vr,')' ))) construction.