I have a data frame of about 3000 rows. I wish to find the longest run of positive and negative numbers.
My example dataframe: df
1 0.502310591
2 -0.247577976
3 -0.307256769 2
4 0.442253678
5 -0.795770351
6 2.08244648
7 -0.01672777
8 -0.164145656 2
9 0.610117365
10 0.014758371
11 0.381105476
12 0.721386493 4
13 -0.363222383
14 0.201409322
15 0.724867214
16 -1.586829584
17 1.066288451
18 0.182824494
19 0.237447191
20 -0.215475797
Longest positive run: 4 Longest negative run: 2
I am following this tutorial: https://ocw.mit.edu/ans7870/18/18.05/s14/html/r-tut-rle.html
I need to fund the longest run of values >0 and also <0. So is there any way to edit the above?
I guess this only finds longest lengths of 1,0? If that case then i would need a helper column ifelse 1,0 to split the negative , positive... then perhaps find longest length with this code:
> df$wins <- ifelse(df$V2 > 0, 1, 0)
> df$loss <- ifelse(df$V2 < 0, 1, 0)
> win <- (c(df$wins))
> max(rle(win)$lengths)
[1] 4
This works for finding maximum wins...
This is for losses:
> print(df$loss)
[1] 0 1 1 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1
> df$loss <- ifelse(df$V2 < 0, 1, 0)
> print(df$loss)
[1] 0 1 1 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1
> loss <- (c(df$loss))
> max(rle(loss)$lengths)
[1] 4
Not sure why it says 4... there is clearly 2 maximum losses, anyone see why? What am i missing, shouldn't the logic of the wins work exactly the same for the loss? I cant see any error in the code...
if value in df$loss is less than 0 print 1 else 0.
make a vector containing the contents of df$loss column
find max length of 1's using max(rle(loss)$lengths)
again result is 4.. however, its clearly 2?