Dataframe 计数成功的值转换

Dataframe 计数成功的值转换,dataframe,awk,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做到这一点 任何帮助都将不胜感激 这个怎么样U和L作为下限和上限传入,而代码的逻辑应该很简单: 如果我们的股价

我有一个数据集,看起来像:

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做到这一点


任何帮助都将不胜感激

这个怎么样
U
L
作为下限和上限传入,而代码的逻辑应该很简单:

  • 如果我们的股价低于
    L
    ,我们就有可能上涨
  • 如果我们在
    U
    以上,我们就有可能下跌
  • 如果我们在U之上,并且我们是上升的候选者,那么我们已经上升了
  • 如果我们低于
    L
    ,并且我们可能会下跌,那么我们已经下跌了
U){falling=1}
如果($0>U&&rising==1){rises+=1;rising=0}
如果($0
像这样:

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
awk-v up=3.8-v low=3.0-c=0'!f&&$0up{f=0;c++}END{print c?c:0}文件
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}
#如果我们处于低位->高位,我将使用变量“f”(标志)来存储
#范围与否。awk为我们自动将变量初始化为0。我们有
#初始化“c”,因为在没有
#结果发现
开始{
c=0
}

#如果当前值的上升和下降不必只发生在一个步骤中,则设置标志,对吗?如中所示,
4.0 3.3 2.8
是一次失败,不是吗?在你的情况下,我的理解是有一个上升(
2.9 3.8
)和一个下降(
4.0 3.3 2.8
)。我喜欢这个答案,因为它解释了等待的相反顺序!你的回答(我更喜欢,啊哈)也可以,不是吗,@hek2mgl?我承认我不确定我是否正确理解OPs要求,因此只是发布了顺序解决方案。很惊讶被接受:)。稍后我将添加它,我并不感到惊讶,因为您的答案使用了
awk
,因为它使用条件而不是操作进行检查。它更好!我不会说条件语句不应该用在动作中。您的代码可读、可维护,因此非常好!当它怀疑时,我会说,在惯用的实现之上,使用更明确、可读的实现是一种好方法。如果你想吸引更多的选票,可能会有所不同:)但有时候这确实是随机的,
c
的初始化可以通过执行
END{print c+0}
@kvantour yeah或
print c?c:0
来删除。但我不喜欢这样,决定初始化它。看起来更干净
# 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}