【问题标题】:R: Aggregating a list of column names and row numbers based off of a condition in a data frameR:根据数据框中的条件聚合列名和行号列表
【发布时间】:2017-04-20 18:30:29
【问题描述】:

按比例缩小我的数据框如下所示:

+---+------------+-------------+
|   |   Label1   |   Label2    |
+---+------------+-------------+
| 1 |      T     |     F       |
| 2 |      F     |     F       |
| 3 |      T     |     T       |
+---+------------+-------------+

我想在列表/向量中聚合并返回它,这样对于所有错误值,我的列表都包含(列名、行号)这样的内容,例如 {(Label1, 2)(Label2, 1) (标签2, 2)}

【问题讨论】:

    标签: r


    【解决方案1】:
    df = data.frame(Label1 = c("T","F","T"), Label2 = c("F", "F", "T"))
    with(data.frame(which(df == "F", arr.ind = TRUE)), paste(names(df)[col], row, sep = ", "))
    #[1] "Label1, 2" "Label2, 1" "Label2, 2"
    

    【讨论】:

    • 您解决方案中的结果与我的预期结果完美匹配。
    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多