Q:

Check and count conditions for following value

I have a dataframe with 18 rows and 25 variables. The values are between 0 and 1. For each row, I want to count the number of times a high value (> than 0.7) is followed by a low value (<0.4) and stored that count in a new column.
So far I have been using:
df$n_calls<-rowSums(df > 0.7)
I know it is possible to use different conditions but in my case it is very important to check that the low value is right after the high value
Here is an example of my df
            1          2          3          4          5          6          7          8          9         10          11
1   0.186158072 0.27738592 0.42165043 0.43501515 0.10918095 0.09976244 0.09571536 0.08674526 0.09239877 0.07523392 0.043679510
2   0.773469188 0.75381254 0.20389633 0.46444408 0.30433377 0.68334244 0.42105103 0.66224478 0.32412056 0.30951402 0.616658953
3   0.201245200 0.26873094 0.25892904 0.38605874 0.68438397 0.30236790 0.51493090 0.66314468 0.68910974 0.59134860 0.625550641
4   0.033746517 0.06388212 0.06978669 0.05517553 0.06032239 0.06736223 0.06514233 0.05133860 0.06034266 0.05702451 0.011144861
5   0.590297759 0.40352955 0.08106493 0.06063485 0.07780428 0.09633069 0.10882515 0.11468680 0.28375374 0.63941033 0.629284574
6   0.165001648 0.31174739 0.36955514 0.47581249 0.65349233 0.66471913 0.58004314 0.50790858 0.51298260 0.18651107 0.501195655
7   0.033164989 0.05678890 0.05941058 0.04139692 0.04660761 0.05452679 0.04939543 0.02780824 0.03680599 0.04645522 0.018496662
8   0.080893779 0.07228276 0.07473865 0.05536056 0.05732153 0.06403365 0.06139970 0.05142047 0.05698089 0.06998986 0.032598440
9   0.557273680 0.49226191 0.63900601 0.37497255 0.72114277 0.37557355 0.34360391 0.37502000 0.41622472 0.46852220 0.410656260
10 -0.004010143 0.03051558 0.04403711 0.02749514 0.04770637 0.05800898 0.05603494 0.04163723 0.04622024 0.04677767 0.007736933
11  0.280273472 0.59839662 0.74167893 0.75352655 0.75108785 0.72345468 0.65395063 0.32957749 0.08357061 0.33165070 0.731228429
12  0.107398713 0.10983041 0.13630594 0.19905651 0.47014034 0.72519345 0.69545405 0.62194265 0.49873996 0.16549282 0.087689371
13  0.164520925 0.22763832 0.50824238 0.59686660 0.68419908 0.66837348 0.62380175 0.20226234 0.11425066 0.09725765 0.078701134
14  0.076934267 0.09684586 0.10703672 0.08436558 0.10789735 0.24130640 0.36615645 0.42805115 0.42937392 0.51390288 0.584757257
15  0.055565174 0.06796064 0.07519020 0.05498454 0.05754891 0.06377643 0.06537049 0.05152625 0.05783594 0.05963775 0.022556411
16  0.126975964 0.19394191 0.53324900 0.60905758 0.67072084 0.61613836 0.55415573 0.18317823 0.13453799 0.09835233 0.067080267
17  0.730333357 0.65759923 0.59045925 0.63148539 0.36305458 0.40829673 0.48734552 0.58647457 0.66968986 0.48312152 0.453863785
18  0.196450179 0.33968393 0.51538678 0.44868341 0.22221050 0.18934329 0.19179838 0.18764290 0.22423578 0.27524872 0.608625015
            12          13         14         15         16          17         18         19         20         21         22
1  0.038553121 0.040081485 0.05358118 0.07403555 0.05091901 0.042299806 0.04322122 0.05587749 0.06881493 0.09753878 0.10462942
2  0.618447812 0.048885425 0.06231155 0.08228801 0.05963307 0.022666894 0.09384802 0.07914030 0.08549148 0.08373159 0.07404309
3  0.179434300 0.679981042 0.69176338 0.74453573 0.70937271 0.289762839 0.17956945 0.68770664 0.73864122 0.73187173 0.34604987
4  0.005094105 0.007952117 0.02076629 0.04174891 0.02129751 0.010066515 0.01454399 0.04337116 0.05259742 0.05795045 0.04533231
5  0.554122074 0.322792638 0.21839661 0.18322419 0.05764354 0.041600287 0.04692187 0.04305403 0.05762126 0.06212474 0.05289008
6  0.719147265 0.481543275 0.20168371 0.19885731 0.27223662 0.587549079 0.66694312 0.76974309 0.45266122 0.23338301 0.09435850
7  0.019041585 0.005380972 0.01856521 0.03947278 0.01221314 0.004858193 0.01322566 0.02001854 0.02755861 0.03889634 0.03102918
8  0.031368415 0.024535386 0.04031225 0.06011198 0.03558484 0.027890723 0.04100022 0.04572906 0.05465957 0.06437218 0.06308497
9  0.290487995 0.109253389 0.09076971 0.11177720 0.08365271 0.074780381 0.07845467 0.08843678 0.12696256 0.15252180 0.16108674
10 0.004599971 0.004843833 0.02327683 0.05022203 0.02867540 0.013674600 0.02376855 0.03408261 0.04563785 0.04991278 0.04216682
11 0.702763718 0.204497547 0.05554607 0.07056242 0.04561622 0.027652748 0.05185238 0.03544719 0.04735368 0.05194280 0.05193089
12 0.087884047 0.068055513 0.07587232 0.09912338 0.09637278 0.085378227 0.09348430 0.09237792 0.10785289 0.22242136 0.28522539
13 0.050134608 0.060945434 0.07203437 0.09687331 0.07316602 0.067771770 0.07634787 0.08154630 0.09157153 0.08930093 0.09904561
14 0.255098748 0.323642069 0.34568802 0.42105224 0.41797424 0.434900416 0.39764147 0.30798058 0.31269146 0.42912436 0.52562571
15 0.015262751 0.027712972 0.03813722 0.07103989 0.05202094 0.040513502 0.04066496 0.23360454 0.34666910 0.62701471 0.61683636
16 0.052436966 0.080045644 0.11447572 0.10672800 0.07924541 0.064626998 0.07234429 0.06744468 0.07878329 0.08901864 0.07953835
17 0.422132751 0.127518376 0.13062324 0.15104667 0.12490013 0.110841862 0.10892834 0.07984952 0.09097741 0.15193027 0.18654107
18 0.662904286 0.247251060 0.20583902 0.32290931 0.47391488 0.574805088 0.64776018 0.73091902 0.27798841 0.35922799 0.36333131
           23         24 n_calls 
1  0.23100480 0.30027592       0  
2  0.07209460 0.06670631       1  
3  0.30800154 0.27452357       2  
4  0.04148986 0.03842700       0  
5  0.05362370 0.05018294       0  
6  0.08703911 0.08242964       0  
7  0.03186000 0.03233006       0  
8  0.05789078 0.05637648       0  
9  0.25593446 0.29909342       1  
10 0.03615961 0.03356159       0 
11 0.05754763 0.06368048       1 
12 0.45794999 0.56138753       0 
13 0.16676533 0.22718405       0 
14 0.63646856 0.29169414       0 
15 0.64039251 0.60901138       0 
16 0.08805636 0.09688941       0 
17 0.36883747 0.41561690       1 
18 0.37085132 0.36292634  

Any idea how to proceed?

A:

We can use the rowSums based on subsetting the dataset by removing the last column, first column so that dimensions will the same and it compares the adjacent columns
rowSums(df[-length(df)] > 0.7 & df[-1] < 0.4)

