Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first column with specific property

Tags:

r

In a data frame, after some calculations, all rows end with a series of 0, as in the (partial) example below:

   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
1  -9  0  0  0  0  0  0  0  0   0   0   0   0   0   0
2   4 -1  1 -1  0 -1  0  0  0   0   0   0   0   0   0
3   3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
4   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0
5  -3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
6  -6  0  0  0  0  0  0  0  0   0   0   0   0   0   0
7   4 -4  1 -1  0 -1  0  0  0   0   0   0   0   0   0
8   3 -3  0  0  0  0  0  0  0   0   0   0   0   0   0
9   3  0  0  0  0  0  0  0  0   0   0   0   0   0   0
10  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0
11 -3  0  0  0  0  0  0  0  0   0   0   0   0   0   0

But:
- some isolated 0 can occur before the starting of the series of 0, as in lines 2 and 7
- some lines are entirely made of 0, as in lines 4 and 10
I would like to create a new column containing the following information:
"in which column does the series of 0 start?"
From the above example, this new column should contain the numbers:

2, 7, 2, 1, 2, 2, 7, 3, 2, 1, 2

I can't figure out how to do this... Thanks for any hint.

like image 925
Andrew Avatar asked Apr 17 '26 18:04

Andrew


1 Answers

Use apply to run rle on each row and get the first index where the value is equal to zero and the length is greater than 1 (start of series).

apply(df, 1, function(x) which(rle(x)$values == 0 & rle(x)$lengths > 1)[1] )
# [1] 2 7 2 1 2 2 7 3 2 1 2

Data

df = structure(list(X1 = c(-9L, 4L, 3L, 0L, -3L, -6L, 4L, 3L, 3L, 
0L, -3L), X2 = c(0L, -1L, 0L, 0L, 0L, 0L, -4L, -3L, 0L, 0L, 0L
), X3 = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), X4 = c(0L, 
-1L, 0L, 0L, 0L, 0L, -1L, 0L, 0L, 0L, 0L), X5 = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X6 = c(0L, -1L, 0L, 0L, 0L, 
0L, -1L, 0L, 0L, 0L, 0L), X7 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), X8 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), X9 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X10 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X11 = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X12 = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L), X13 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), X14 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), X15 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", 
"X12", "X13", "X14", "X15"), class = "data.frame", row.names = c(NA, 
-11L))
like image 182
d.b Avatar answered Apr 19 '26 09:04

d.b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!