Home > OS >  Double If else condition
Double If else condition

Time:06-12

I have

sb_bs <- structure(c(1.12091846661435, 1.18009738415273, 1.11298146308129, 1.01543479350125, 1, 0.951241085342297, 1.13080707303053, 
1.02485190419514, 1.1642497054576, 
1.09758768260489, 1.03143204936253, 
0.962263806546468, 1.13646386224131, 
1.05528801854607, 1, 1.0099677412001, 1.27733703174185, 1.08617651501537, 1.09642463008668, 0.962776610165752, 1.09650777274647, 1.02585985559578, 1, 0.999386657988493, 1.13956770512708, 
1.02801721288972, 1.08668114989057, 1.00619821265949, 1.18651806206661, 1.10567560388514,  1.13871340983327, 1.0641141127524, 1.14138250428664, 1.07165852394532, 1.14672434661463, 1.05773939982737, 1.03068291407157, 0.969220946334891, 1, 0.996545720184137, 1.16201672634516, 1.14932701076931, 1.03239099834277, 0.952596628890447, 1.1015668980208, 1.01070602087167, 1.06446487490351, 0.951035926316207, 1.123214561944, 1.02175192620079, 1.05854401298598, 0.955952082548675, 0.93666054698966, 0.78951026731046, 1.07778659045028, 1.00399655566558, 1.17991276258629, 1.08980598472291, 1, 0.988417315255171,
1.17162519954207, 1.08543591629641, 1.16332270621974, 1.04640496770377,
1.07628139834293, 0.660300975816023, 1.18023510375109, 1.06381322815647, 1, 0.985519330082672, 1, 1, 1.11167375547688, 0.989782753394904, 1.19539027283808, 1.08094458111008, 1.08360592510174, 0.995189438996505, 1.12905282067727, 1.01854704758263, 1, 0.999513001091598, 1.12582107084784, 
1.03809878502361, 1.20383394437524, 1.11282621100709, 1.11890798835696, 
1.02069677382081, 1.1718663965017, 1.14932701076931, 1.10567893629417, 
1.01280861843888, 1, 0.997229184135707, 1.36291711157704, 1.21997211505057, 1, 0.995717127378999, 
1.11925862571016, 1.0265000455924, 1.19813504773696, 1.08093802496861, 1.09083155239671, 1.00733022178486, 1.08576682175743, 1.02765522155381, 1.14435310285118, 1.0368424021034, 1.10728085614489, 1.03369127812722, 1, 1, 1, 1.0010343593862, 1.09869128313384, 1.00986110345981, 1.16569766816216, 1.06809300727111, 1.08691881875315, 0.987756631843919, 1, 0.995593919635795, 1.21060416986701, 1.11431887118973, 1.17372870338765, 1.08555976717623, 1.12767749680449, 
1.05498213441735, 1.20877696937163, 1.13951692732442, 1.20557208134671, 
1.10161115189349, 1.14075335025544, 0.986457883171349, 1.10161115189349, 
1.01901529496121, 1, 1.00246940215672, 1.09886441422695, 0.991136014242189, 1.13353139262452, 0.999202603818596, 1.25545764969252, 1.16709504376582, 1.253813300474, 1.18586034201829, 1.36139434951718, 1.27643122050034, 1.11532774927192, 1.0249592726384, 1, 0.98818636452525, 1.11734646265344, 1.0291275567626, 1.09096560662508, 
0.995745426192434, 1, 1, 1.10951296399544, 1.01652115456415, 1.16550460579752, 1.11388837421179, 1, 0.997573772176866, 1, 0.98464674069702, 1, 0.997250277028562, 1, 1, 1, 0.999386657988493,
1.12580323330241, 1.0397394609463, 1, 0.999386657988493, 1, 0.997353796391289, 1.14155615975893, 1.05795767043203, 1, 0.972953439553992, 1, 1.00392875483313, 1, 0.995937255059779, 1, 1.00106654711201, 1.08073289044227, 1.02175192620079), .Dim = c(2L, 95L))

and I want run this if-else condition

if ((sb_bs[1,] == 1) || (sb_bs[2,] == 1)) {
  rev_sell_buy <- 0
} else {
  rev_sell_buy <- sb_bs[1,] - sb_bs[2,]
}

but it consider only the second condition (sb_bs[2,] == 1) in the result. I need that if the value in the first or in the second row is 1, it return me 0. Else the difference sb_bs[1,] -(sb_bs[2,]). How can I do? Thx to everyone

CodePudding user response:

Try this

rev_sell_buy <- apply(sb_bs , 2 ,
 function(x) if((x[[1]] == 1) | (x[[2]] == 1)) 0 else x[[1]] - x[[2]])

CodePudding user response:

You do not really need any if-else, but could do:

rev_sell_buy <- sb_bs[1,] - sb_bs[2,]
rev_sell_buy[(sb_bs[1,] == 1) | (sb_bs[2,] == 1)] <- 0

Beware of the difference between | and || (see e.g. What is the difference between || and | in R ?)

  • Related