dataframe - 计算成功的值转换

标签 dataframe awk

我有一个数据集,如下所示:

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/

相关文章:

python - 匹配列并 append 到数据框,Python 3.6

python - 在多列中合并具有相同值的两个数据框

dataframe - 在 Julia DataFrame 的堆栈函数中将measure_vars 放在id_vars 之前的原因是什么?

python - Pandas 过滤或删除行多个条件

bash - 如何使用 bash 用前导零填充文件名

unix - 从 cut 命令强制输出字段的顺序

csv - 有没有办法在 writetable() 中使用字符串作为分隔符 - Julia

linux - 如何使用shell从另一个文件中的相应条目中减去一个文件中的每个条目

linux - 如何计算 awk 中输出的实例数?

linux - 如何将文件名作为变量从 shell 脚本传递给 awk 命令