Count the no of Consecutive no greater then threshold value but groupwise in a Given data frame
By : corona newbie
Date : March 29 2020, 07:55 AM
wish of those help We can use data.table. Convert the 'data.frame' to 'data.table' (setDT(df)), create a grouping variable using the runlengthid of the logical vector (temp >= 40), Grouped by 'tag', 'grp' and set the i with logical condition, we assign 'status' as the sequence of rows (seq_len(.N)), and convert the 'NA' elements in 'status' to 0 code :
library(data.table)
setDT(df)[, grp := rleid(temp >= 40)][temp >= 40, status := seq_len(.N) , .(tag, grp)
][is.na(status), status := 0][]
head(df, 20)
# tag temp grp status
# 1: 1 43 1 1
# 2: 1 44 1 2
# 3: 1 45 1 3
# 4: 1 41 1 4
# 5: 1 43 1 5
# 6: 1 38 2 0
# 7: 1 40 3 1
# 8: 1 41 3 2
# 9: 1 39 4 0
#10: 1 37 4 0
#11: 2 37 4 0
#12: 2 39 4 0
#13: 2 45 5 1
#14: 2 42 5 2
#15: 2 41 5 3
#16: 2 43 5 4
#17: 2 44 5 5
#18: 2 39 6 0
#19: 2 38 6 0
#20: 2 37 6 0
df$status < with(df, ave(temp >= 40, tag, FUN = function(x) {
rl < rle(x)
with(rl, sequence(lengths) * rep(values, lengths))}))
df$status
#[1] 1 2 3 4 5 0 1 2 0 0 0 0 1 2 3 4 5 0 0 0 1 2 3 4 5
#[26] 0 1 2 0 0 0 0 1 2 3 4 5 0 0 0 1 2 3 4 5 0 1 2 0 0 0 0 1 2 3 4 5 0 0 0

Removing points from list if distance between 2 points is below a certain threshold
By : Dion Ng
Date : March 29 2020, 07:55 AM
I wish this helpful for you Not a fancy oneliner, but you can just iterate the values in the list and append them to some new list if the current value is greater than the last value in the new list, using [1]: code :
lst = range(10)
diff = 3
new = []
for n in lst:
if not new or abs(n  new[1]) >= diff:
new.append(n)
if not new or ((n[0]new[1][0])**2 + (n[1]new[1][1])**2)**.5 >= diff:
lst = [complex(x,y) for x,y in lst]
new = []
for n in lst:
if not new or abs(n  new[1]) >= diff: # same as in the first version
new.append(n)
print(new)

find points in vector where change is greater than threshold
By : Nico
Date : March 29 2020, 07:55 AM
wish helps you I want to find positions in a vector where the value differs by more than some threshold value from an earlier point in the vector. The first changepoint should be measured relative to the first value in the vector. Subsequent changepoints should be measured relative to the previous changepoint. , Implementing the same code in Rcpp can help with speed. code :
library(Rcpp)
cppFunction(
"IntegerVector foo(NumericVector vect, double difference){
int start = 0;
IntegerVector changepoints;
for (int i = 0; i < vect.size(); i++){
if((vect[i]  vect[start]) > difference  (vect[start]  vect[i]) > difference){
changepoints.push_back (i+1);
start = i;
}
}
return(changepoints);
}"
)
foo(vect = x, difference = mindiff)
# [1] 17 25 56 98 108 144 288 297 307 312 403 470 487
identical(foo(vect = x, difference = mindiff), changepoints)
#[1] TRUE
#DATA
set.seed(123)
x = cumsum(rnorm(1e5))
mindiff = 5.0
library(microbenchmark)
microbenchmark(baseR = {start = x[1]
changepoints = integer()
for (i in 1:length(x)) {
if (abs(x[i]  start) > mindiff) {
changepoints = c(changepoints, i)
start = x[i]
}
}}, Rcpp = foo(vect = x, difference = mindiff))
#Unit: milliseconds
# expr min lq mean median uq max neval cld
# baseR 117.194668 123.07353 125.98741 125.56882 127.78463 139.5318 100 b
# Rcpp 7.907011 11.93539 14.47328 12.16848 12.38791 263.2796 100 a

Find three consecutive numbers greater than threshold groupwise in R
By : CookieMonsderp
Date : March 29 2020, 07:55 AM
I hope this helps . If it's above the threshold and it's the third such value in a row, capture the index in ends. Select the first index in ends and add one to get the index of the return time. (There may be more than 1 such group of 3 and therefore more than one element of ends. In this case, the first end needs to be used.) Note: In your example, the speed at return time is always above the threshold. This code does not check that as a condition at all, but simply gives the first time after three rows with speeds above threshold (regardless of whether the speed at that time is still above the threshold). code :
library(data.table)
setDT(df)
speed_thresh < 35
df[, {above < Speed > speed_thresh
ends < which(above & rowid(rleid(above)) == 3)
.(Return_Time = Time[ends[1] + 1])}
, Group]
# Group Return_Time
# 1: 1 35
# 2: 2 25
# 3: 3 NA
df < fread('
Group Time Speed
1 5 25
1 10 23
1 15 21
1 20 40
1 25 42
1 30 52
1 35 48
1 40 45
2 5 22
2 10 36
2 15 38
2 20 46
2 25 53
3 5 45
3 10 58
')

Check if there are 3 consecutive values in an array which are above some threshold
By : amie
Date : March 29 2020, 07:55 AM
Does that help Approach #1 Use convolution on the mask of boolean array obtained after comparison  code :
In [40]: a # input array
Out[40]: array([ 1, 3, 4, 5, 60, 43, 53, 4, 46, 54, 56, 78])
In [42]: N = 3 # compare N consecutive numbers
In [44]: T = 40 # threshold for comparison
In [45]: np.flatnonzero(np.convolve(a>T, np.ones(N, dtype=int),'valid')>=N)
Out[45]: array([4, 8, 9])
In [77]: from scipy.ndimage.morphology import binary_erosion
In [31]: np.flatnonzero(binary_erosion(a>T,np.ones(N, dtype=int), origin=(N//2)))
Out[31]: array([4, 8, 9])
m = a>T
out = np.flatnonzero(m[:2] & m[1:1] & m[2:])
In [78]: a
Out[78]: array([ 1, 3, 4, 5, 60, 43, 53, 4, 46, 54, 56, 78])
In [79]: a = np.tile(a,100000)
In [80]: N = 3
In [81]: T = 40
# Approach #3
In [82]: %%timeit
...: m = a>T
...: out = np.flatnonzero(m[:2] & m[1:1] & m[2:])
1000 loops, best of 3: 1.83 ms per loop
# Approach #1
In [83]: %timeit np.flatnonzero(np.convolve(a>T, np.ones(N, dtype=int),'valid')>=N)
100 loops, best of 3: 10.9 ms per loop
# Approach #2
In [84]: %timeit np.flatnonzero(binary_erosion(a>T,np.ones(N, dtype=int), origin=(N//2)))
100 loops, best of 3: 11.7 ms per loop

