【问题标题】:IF statement in r Dataframer Dataframe 中的 IF 语句
【发布时间】:2018-05-30 00:25:55
【问题描述】:

如何编写带有两个条件的 IF 语句来检索相应的值,例如满足这些条件的人的姓名?

这是我的声明:

if(any((Basketball$TwoPoint >= 45) && (Basketball$ThreePoint >= 45))) 
{
  print(Basketball$Person == (Basketball$TwoPoint >= 45) && (Basketball$ThreePoint >= 45))
}

我知道我有 2 个人的投篮命中率高于 45 [%],因为这个数据框只有 7 行和 7 列,所以我能够验证这是真的。

【问题讨论】:

    标签: r if-statement dataframe


    【解决方案1】:

    数据框很好,因为您可以在数据框内完成。

    df <- df[which(df$col1 == 'val1' & df$col2 == 'val2'),]
    

    这将是一种快速简便的方法。

    (另外,如果你想要一些更优雅的 dplyr)

    【讨论】:

    • 甚至不需要which()df$col1 == 'val1' &amp; df$col2 == 'val2' 将给出一个逻辑向量,它将对行进行子集化。
    • 感谢 Eric 和 Thelatemail 的回复!我一定会调查 dplyr。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2016-03-05
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多