我有一个数据集,如下所示:
2.0
2.6
3.2
2.9
3.8
3.5
3.9
3.4
4.0
3.3
2.8
2.9
我想对 3.8 和 3.0 的数据集应用上限和下限。使用这些界限,我想计算从低于 3.0 到高于 3.8 的成功转换,但不计算数据刚刚超过 3.0 的实例。我还想计算数据从高于 3.8 到低于 3.0 的反向事件,但不计算从高于 3.8 开始低于 3.8 然后返回高于 3.8 的情况。
有没有办法用 awk 来做到这一点?
任何帮助将不胜感激。
最佳答案
像这样:
awk -v up=3.8 -v low=3.0 -c=0 '!f&&$0<=low{f=1}f&&$0>up{f=0;c++}END{print c?c:0}' file
1
说明:
# I'm using a variable 'f' (flag) to store if we are within a low -> up
# range or not. awk auto-initialized the variable with 0 for us. We have
# to initialize 'c', because it might otherwise not been set when no
# result is found
BEGIN {
c=0
}
# set the flag if the current value <= the lower boundary
!f && $0<=low {
f=1
}
# reset the flag if the flag is set and we surpass the upper boundary
# increment the count 'c'
f && $0>up{
f=0
c++
}
# Print c at the end of input. Note that c
END{print c}
关于dataframe - 计算成功的值转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60085034/