在向量上使用 If else 条件 [英] Using If else conditions on Vectors
本文介绍了在向量上使用 If else 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到以下逻辑问题.基本上,我有一个 MS 和 Apple 的股票数据框.我想根据某些价格比较来执行买卖条件.但是 R 不允许我将 If-Else 条件与向量一起使用,我该如何克服这种情况
I am having issues with the following piece of logic. Basically, i have a data frame of stocks for MS and Apple. I want to execute buy and sell conditions based on certain price comparisons. But R does not allow me to use If-Else conditions with vectors how do I overcome this scenario
if (mydatastocks$MS<120){
if (mydatastocks$MS>110 & mydatastocks$MS<120){
print("buy small")
}else{
print("Buy Huge")
}
} else{
if(mydatastocks$MS>120)
print("Ignore")
}
推荐答案
将顺序分配给数据框中的另一列可能最有意义:
It might make the most sense to assign the order to another column in your data frame:
mydatastocks$order <- ifelse(mydatastocks$MS > 110 & mydatastocks$MS < 120,
"buy small",
ifelse(mydatastocks$MS <= 120, "buy huge", "ignore"))
这篇关于在向量上使用 If else 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文