Consecutive positive and negative numbers

Hi Guys,

I am looking for a help to get consecutive positive and negative numbers on these lines.

columns : M-1 to M-6
In first row,you can see there are 2 consecutive +ve's and corresponding result needs to be calculated in Consecutive +ve column and vice versa. I tried using dpylr, but i was not able to formulate for all these rows(which is a part of a much bigger data set), many of the queries which got answered in respective forums were actioning only 1 column i.e checking results for "M-6" column alone. Please help me out.

Can this be done with the help of a for and if else loop as well?

M-6 M-5 M-4 M-3 M-2 M-1 Consecutive+ve Consecutive -ve
1 1 0 0 0 1 2 0
4 -4 0 0 10 10 2 0
0 0 1 1 1 0 3 0
5 0 1 1 1 1 4 0
-1 -1 1 1 1 1 4 2
0 0 0 0 0 24 0 0
0 0 0 0 0 2 0 0
3 0 0 2 0 2 0 0
0 -1 0 1 1 1 3 0
3 0 0 0 0 2 0 0
-4 6 -3 5 8 2 3 0
-72 -27 -10 13 42 7 3 3

Hope this would help.

#> # A tibble: 6 x 8
#>      M1    M2    M3    M4    M5    M6 `Consecutive.+ve` `Consecutive.-ve`
#>   <int> <int> <int> <int> <int> <int>             <int>             <int>
#> 1     1     1     0     0     0     1                 2                 0
#> 2     4    -4     0     0    10    10                 2                 0
#> 3     0     0     1     1     1     0                 3                 0
#> 4     5     0     1     1     1     1                 4                 0
#> 5    -1    -1     1     1     1     1                 4                 2
#> 6     0     0     0     0     0    24                 0                 0

Code added:

data.frame( M1 = c(1,4,0,5,-1,0,-4,-72),
M2 = c(1,-4,0,0,-1,0,6,-27),
M3 = c(0,0,1,1,1,0,-3,-10),
M4 = c(0,0,1,1,1,0,5,13),
M5 = c(0,10,1,1,1,0,8,42),
M6 = c(1,10,0,1,1,24,2,7),
Results_positive = c(2,2,3,4,4,0,3,3),
Results_negative = c(0,0,0,0,2,0,0,3))

My earlier function woks fine with this. Still, posting a reprex, and deleting old post as that's now unnecessary.

sample_data <- data.frame(M1 = c(1, 4, 0, 5, -1, 0, -4, -72),
                          M2 = c(1, -4, 0, 0, -1, 0, 6, -27),
                          M3 = c(0, 0, 1, 1, 1, 0, -3, -10),
                          M4 = c(0, 0, 1, 1, 1, 0, 5, 13),
                          M5 = c(0, 10, 1, 1, 1, 0, 8, 42),
                          M6 = c(1, 10, 0, 1, 1, 24, 2, 7))

results_positive_expected <- c(2, 2, 3, 4, 4, 0, 3, 3)
results_negative_expected <- c(0, 0, 0, 0, 2, 0, 0, 3)

runs <- apply(X = sample_data,
              MARGIN = 1,
              FUN = function(t) rle(x = sign(x = t)))

get_maximum_consecutive_lengths <- function(run_data, value_sign)
{
    runs_of_sign <- with(data = run_data,
                         expr = lengths[values == value_sign])
    
    if (length(x = runs_of_sign) == 0)
        return(0)
    
    maximum_run <- max(runs_of_sign)
    
    if (maximum_run == 1)
        return(0)
    
    return(maximum_run)
}

sample_data$results_positive <- vapply(X = runs,
                                       FUN = get_maximum_consecutive_lengths,
                                       FUN.VALUE = numeric(length = 1L),
                                       value_sign = 1)
sample_data$results_negative <- vapply(X = runs,
                                       FUN = get_maximum_consecutive_lengths,
                                       FUN.VALUE = numeric(length = 1L),
                                       value_sign = -1)

all.equal(target = results_positive_expected,
          current = sample_data$results_positive)
#> [1] TRUE
all.equal(target = results_negative_expected,
          current = sample_data$results_negative)
#> [1] TRUE

sample_data
#>    M1  M2  M3 M4 M5 M6 results_positive results_negative
#> 1   1   1   0  0  0  1                2                0
#> 2   4  -4   0  0 10 10                2                0
#> 3   0   0   1  1  1  0                3                0
#> 4   5   0   1  1  1  1                4                0
#> 5  -1  -1   1  1  1  1                4                2
#> 6   0   0   0  0  0 24                0                0
#> 7  -4   6  -3  5  8  2                3                0
#> 8 -72 -27 -10 13 42  7                3                3

Created on 2020-08-13 by the reprex package (v0.3.0)

Hope this helps.

1 Like

This works perfect! Thank you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.