I have this example data
library(quantmod)
getSymbols("NOK",from="2013-01-01",to="2014-05-01",src="yahoo","getSymbols.warning4.0"=FALSE)
data<-NOK
w1<-1
L_dO<-data[,1]
L_dC<-data[,4]
L_Profit_L_1<-((lag(L_dC,-1)-lag(L_dO,-1))/(lag(L_dO,-1)))*100
L_Profit_L_2<-((lag(L_dC,-2)-lag(L_dO,-1))/(lag(L_dO,-1)))*100
L_Profit_L_3<-((lag(L_dC,-3)-lag(L_dO,-1))/(lag(L_dO,-1)))*100
L_Profit_L_4<-((lag(L_dC,-4)-lag(L_dO,-1))/(lag(L_dO,-1)))*100
L_Profit_L_5<-((lag(L_dC,-5)-lag(L_dO,-1))/(lag(L_dO,-1)))*100
L_Profit_L_all<-ifelse(L_Profit_L_1>w1,L_Profit_L_1,
ifelse(L_Profit_L_2>w1,L_Profit_L_2,
ifelse(L_Profit_L_3>w1,L_Profit_L_3,
ifelse(L_Profit_L_4>w1,L_Profit_L_4,
ifelse(L_Profit_L_5>w1,L_Profit_L_5,L_Profit_L_5)))))
What am I interested in is L_Profit_L_all
, but I see this is a bit strange and slow way to write it. I have tried to vectorize it like
L_Profit_L_all<-ifelse(c(L_Profit_L_1>w1,L_Profit_L_2>w1,L_Profit_L_3>w1,L_Profit_L_4>w1,L_Profit_L_5>w1),c(L_Profit_L_1,L_Profit_L_2,L_Profit_L_3,L_Profit_L_4,L_Profit_L_5),L_Profit_L_5)
But th result is not the same. I want it to work in right order, i.e. if the first if condition is TRUE, then return first else condition (and don't care about if another condition is TRUE which is able to do the first code) Any straightforward how to achieve it? I have a huge dataset so every ms is good to save. Thanks