比较数据框R中每行的列

时间:2019-03-07 14:27:56

标签: r dataframe for-loop if-statement

我有一个3列和几千行的数据框。现在,我想比较每行列中的值,以查看这些值是否遵循特定趋势。

for(row in 1:nrow(Df)) {
  if (Df$Value1 > Df$Value2 & Df$Value2 > Df$Value3){
    print("Downward slope")
  } else if (Df$Value1 < Df$Value2 & Df$Value2 < Df$Value3){
    print("Upward slope")
  } else if (Df$Value1 > Df$Value2 & Df$Value2 < Df$Value3){
    print("Dip")
  } else if (Df$Value1 < Df$Value2 & Df$Value2 > Df$Value3){
    print("Peak")
  }
}

在此示例中,数据帧的第1行为向上倾斜,而第3行为向下倾斜。

我写了这个for循环来遍历整个数据帧。

import UIKit

class ViewController: UIViewController {

    lazy var scrollView : UIScrollView = {
        let sv = UIScrollView(frame: self.view.bounds)
        sv.translatesAutoresizingMaskIntoConstraints = false
        sv.delegate = self
        return sv
    }()

    lazy var contentView : UIView = {
        let v = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width * 4, height: self.view.bounds.height))
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    lazy var label : UILabel = {
        let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
        lbl.numberOfLines = 0
        lbl.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
        lbl.minimumScaleFactor = 0.5
        lbl.adjustsFontSizeToFitWidth = true
        lbl.font = UIFont.systemFont(ofSize: 22)
        lbl.translatesAutoresizingMaskIntoConstraints = false
        return lbl
    }()

    var widthConstraint : NSLayoutConstraint?
    var heightConstraint : NSLayoutConstraint?
    var startingHeight : CGFloat = 0
    var startingWidth : CGFloat = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        //first scrollview
        self.view.addSubview(scrollView)
        pinToAllSides(target: scrollView)

        //now content view
        self.scrollView.addSubview(contentView)
        contentView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 2).isActive = true
        contentView.heightAnchor.constraint(equalTo: self.scrollView.heightAnchor, multiplier: 1).isActive = true
        contentView.backgroundColor = .green
        pinToAllSides(target: contentView)
        scrollView.layoutIfNeeded()

        //now the label
        self.scrollView.addSubview(label)
        label.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor, constant: 20).isActive = true
        label.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: 60).isActive = true
        label.backgroundColor = .red
        widthConstraint = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.view.bounds.width/2)

        heightConstraint = NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 300)
        if let wc = widthConstraint,
            let hc = heightConstraint{
            startingHeight = hc.constant
            startingWidth = wc.constant
            label.addConstraint(wc)
            label.addConstraint(hc)
        }

    }

    func pinToAllSides(target:UIView){
        guard let superview = target.superview else{
            return
        }
        target.leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
        target.trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true
        target.topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
        target.bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
    }

    var hasHappenedOnce : Bool = false
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if hasHappenedOnce == false{
            hasHappenedOnce = true
            self.scrollView.contentOffset = .zero
        }
    }
}

extension ViewController : UIScrollViewDelegate{

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //hopefully it is laggy due to simulator but for the label i would ditch constraints myself
        self.widthConstraint?.constant = max(startingWidth, self.scrollView.contentOffset.x * 1.1 + startingWidth)

        let height = startingHeight - self.scrollView.contentOffset.x
        self.heightConstraint?.constant = height
        label.updateConstraints()
    }
}

现在,这可能已经很低效了,但是甚至不起作用。运行此命令时出现错误:

  

该条件的长度> 1,并且将仅使用第一个元素;该条件的长度> 1,并且将仅使用第一个元素[1]“向上倾斜”

     

该条件的长度> 1,并且仅使用第一个元素;该条件的长度> 1,并且仅使用第一个元素[1]“向上倾斜”

我如何使类似的东西起作用?

1 个答案:

答案 0 :(得分:0)

最好使用矢量化ifelse而不是循环

ifelse(Df[,1] >  Df[,2],
  ifelse(Df[,2] >  Df[,3], 'down', 'dip'),
  ifelse(Df[,2] >  Df[,3], 'peak', 'up'))
#[1] "up"   "up"   "down" "down" "up"   "peak" "dip"  "peak"