在Vector上使用If else条件 [英] Using If else conditions on Vectors

查看:75
本文介绍了在Vector上使用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"))

这篇关于在Vector上使用If else条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆