Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

dplyr - detecting sharp bursts in several variables simultaneously in R

i try be clearly. here data

dt=structure(list(x1 = c(28.5, 25.5, 28, 27, 29), x2 = c(28.5, 25.5, 
25, 25.5, 29), x3 = c(8, 5.5, 8.5, 7.5, 8), x4 = c(8.5, 6.5, 
6.5, 5.5, 8), x5 = c(64L, 55L, 54L, 55L, 60L), x6 = c(63.5, 54.5, 
53.5, 54.5, 60), x7 = c(1028L, 1010L, 1008L, 1010L, 1020L), x8 = c(1027L, 
1009L, 1007L, 1009L, 1020L)), class = "data.frame", row.names = c(NA, 
-5L))

I need to find row where at the same time, MINIMUM (at least) 2 of the above variables (x1-x8) changed sharply in the values simultaneous. For example abruptly and simultaneously (this is important) grew up x2, x4, x8

and no matter how much they jumped up, the very fact of a simultaneous (for several variables at the same time) and abrupt event. So we can calculate difference(delta) for x2= 25.5-28.5 = -3

    x2   x4  x8
1 -3.0 -2.0 -18
2 -0.5  0.0  -2
3  0.5 -1.0   2

in general, the difference between before after for each variable is even, x8 jumped sharply down, but others are in their range, but it is necessary that all at once or at least 2 variables. And this we can observe in last row.

 4  3.5  2.5  11  changes last row

Is it possible to set the flag = 1 for row where we see extreme grow.

    x1   x2  x3  x4 x5   x6   x7   x8 flag
1 28.5 28.5 8.0 8.5 64 63.5 1028 1027    0
2 25.5 25.5 5.5 6.5 55 54.5 1010 1009    0
3 28.0 25.0 8.5 6.5 54 53.5 1008 1007    0
4 27.0 25.5 7.5 5.5 55 54.5 1010 1009    0
5 29.0 29.0 8.0 8.0 60 60.0 1020 1020    1

if this is not possible, then is it possible to set that if at least 2 variables among x1-x4 grew up on 2 mm up and and at the same time variable x7-x8 grew up on 10 mm then flag 1 ?

question from:https://stackoverflow.com/questions/65926328/detecting-sharp-bursts-in-several-variables-simultaneously-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code uses Base R methods to

(a). calculate the differences for each column via an apply() command and then

(b). counts for every row whether more than two variables have an absolute change that is bigger than 3 (threshold value) and sets a flag 1 if that is the case and a flag 0 otherwise.

You can always change the threshold value to another value of course, e.g. 4 or 5.

Code:

threshold <- 3
flags <- dt %>% 
         apply(., 2, diff) %>% 
         apply(., 1,  
                  function(x) 
           ifelse(length(x[abs(x) > threshold]) > 1, 
                     1, 
                     0))
dt$flag <- c(0, flags)
dt

Note that we always set the first value of the flag vector as 0 since we do not know the value in t = - 1.

Output:

    x1   x2  x3  x4 x5   x6   x7   x8 flag
1 28.5 28.5 8.0 8.5 64 63.5 1028 1027    0
2 25.5 25.5 5.5 6.5 55 54.5 1010 1009    1
3 28.0 25.0 8.5 6.5 54 53.5 1008 1007    0
4 27.0 25.5 7.5 5.5 55 54.5 1010 1009    0
5 29.0 29.0 8.0 8.0 60 60.0 1020 1020    1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...