仅返回与两个条件过滤器匹配的值,其中name列可以多次使用

时间:2018-03-02 17:09:24

标签: r dplyr

我试图只返回与两个条件过滤器语句匹配的行 如果我group_by名称并且名称在多行中使用,我将获得具有该名称的每一行。

override func viewWillAppear(_ animated: Bool) {
    tableView.delegate = self

    loadCryptoArray()

    tableView.reloadData()
}

func loadCryptoArray() {

    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let archiveURL = documentsDirectory.appendingPathComponent("cryptosArray").appendingPathExtension("plist")
    let propertyListDecoder = PropertyListDecoder()
    if let retrievedArrayData = try? Data(contentsOf: archiveURL),
        let decodedArray = try?
            propertyListDecoder.decode(Array<Cryptos>.self, from: retrievedArrayData) {
        cryptosArray = decodedArray
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cryptosArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let crypto = cryptosArray[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell
    cell.setCrypto(crypto: crypto)
    cell.delegate = self
    cell.amountTextField.delegate = self

    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 85
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        cryptosArray.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)

        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let archiveURL = documentsDirectory.appendingPathComponent("cryptosArray").appendingPathExtension("plist")
        let propertyListEncoder = PropertyListEncoder()
        let encodedArray = try? propertyListEncoder.encode(cryptosArray)
        try? encodedArray?.write(to: archiveURL, options: .noFileProtection)

    }
}

我的表是:

func cellAmountEntered(_ walletTableViewCell: WalletTableViewCell) {

    if walletTableViewCell.amountTextField.text == "" {
        return
    }

    let str = walletTableViewCell.amountTextField.text
    let formatter = NumberFormatter()
    formatter.locale = Locale(identifier: "en_US")
    let dNumber = formatter.number(from: str!)
    let nDouble = dNumber!
    let eNumber = Double(truncating: nDouble)
    walletTableViewCell.amountLabel.text = String(format:"%.8f", eNumber)

    let indexPath = tableView.indexPath(for: walletTableViewCell)
    let crypto = cryptosArray[(indexPath?.row)!]

    crypto.amount = String(format:"%.8f", eNumber)

    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let archiveURL = documentsDirectory.appendingPathComponent("cryptosArray").appendingPathExtension("plist")
    let propertyListEncoder = PropertyListEncoder()
    let encodedArray = try? propertyListEncoder.encode(cryptosArray)
    try? encodedArray?.write(to: archiveURL, options: .noFileProtection)

    print("amount", crypto.amount)

    walletTableViewCell.amountTextField.text = ""

    //updateWalletValueLabel()
}

如果我运行此代码:

name <- c("Jeff", "Jeff", "Jeff", "Jeff", "Jill", "Jill", "Jill", "Jill", "Angie", "Angie", "Angie")

total <- c(123, 234, 432, 321,  345, 456, 654, 543, 567, 678, 789)

code <- c("abc", "xyz", "lmn", "opq", "abc", "xyz", "txy", "jkl",  "xyz", "tup", "rst")

table <- data.frame(name, total, code)   

这给了我:

name  total code
Jeff  123   abc
Jeff  234   xyz
Jeff  432   lmn
Jeff  321   opq
Jill  345   abc
Jill  456   xyz
Jill  654   txy
Jill  543   jkl
Angie 567   xyz
Angie 678   tup
Angie 789   rst

我认为问题是group_by(名称),但如果我没有group_by(名字)我就不能使用&amp;过滤器的同一列上的声明 我想输出:

library(dplyr)  
test1 <- table %>% group_by(name) %>% 
                   filter("abc" %in% code & "xyz" %in% code)   

1 个答案:

答案 0 :(得分:0)

您可以扩展条件以指定。

"_revs_info":[
  {"rev":"14-048c3d19d53fa1477ebc33d45914dd26","status":"available"},
  {"rev":"13-b16eef70a8247298259d293d940edd9a","status":"missing"}
]