【问题标题】:Extract the column names for each row which meets a condition [duplicate]提取满足条件的每一行的列名[重复]
【发布时间】:2018-11-01 17:59:15
【问题描述】:
d <- structure(
  list(
    Cl = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    SaCl = c(0, 1, 0, 0,0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0), 
    SiCl = c(0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L, 0L), 
    ClLo = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    SiClLo = c(0L, 0L, 0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    SaClLo = c(1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1), 
    SaLo = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    SaSiLo = c(0L, 0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    SiLo = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    LoSa = c(0L, 0L, 0L, 0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Sa = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L, 0L, 0L, 0L, 0L, 0L)
  ), 
  row.names = c(NA, 20L),
  class = "data.frame"
)

每一行只有一个1。我想提取每行有 1 的列名,这样我的数据框看起来像

row.id | names
-------+-------
     1 | SaClLo
     2 | SaCl
     3 | SaClLo
     4 | SaClLo

我尝试对每一行运行一个函数

apply(d, 1, function(x) colnames(x)[x == 1])

这是给我NULL

【问题讨论】:

    标签: r apply


    【解决方案1】:

    对于每一行,我们找出哪一列的值为 1,然后为该行选择 colnames 的值。然后我们将其转换为data.frame

    data.frame(names = apply(d, 1, function(x) colnames(d)[which(x == 1)]))
    
        names
    1  SaClLo
    2    SaCl
    3  SaClLo
    4  SaClLo
    ...
    

    或者,您可以通过tibble::rowname_to_column() 运行它以将row.id 从行名更改为列。

    data.frame(names = apply(d, 1, function(x) colnames(d)[which(x == 1)])) %>%
        tibble::rownames_to_column()
    
       rowname  names
    1        1 SaClLo
    2        2   SaCl
    3        3 SaClLo
    4        4 SaClLo
    ...
    

    【讨论】:

      【解决方案2】:

      使用max.col 查找1s 的位置,并使用此向量选择相应的列名。

      data.frame(row.id = 1:nrow(d),
                 names = names(d)[max.col(d)])
      #   row.id  names
      #1       1 SaClLo
      #2       2   SaCl
      #3       3 SaClLo
      #4       4 SaClLo
      #...
      

      【讨论】:

        【解决方案3】:

        which 的一个鲜为人知的功能是你的朋友:

        > which(d==1, arr.ind=TRUE)
           row col
        2    2   2
        11  11   2
        15  15   2
        13  13   4
        ...
        

        第二列是你需要的信息:

        > arr_indices <- which(d == 1, arr.ind = TRUE)
        > colnames(d)[ arr_indices[, 2] ]
         [1] "SaCl"   "SaCl"   "SaCl"   "ClLo"   "SaClLo" "SaClLo" "SaClLo" "SaClLo"
         [9] "SaClLo" "SaClLo" "SaClLo" "SaClLo" "SaClLo" "SaClLo" "SaClLo" "SaClLo"
        [17] "SaClLo" "SaClLo" "SaClLo" "SaClLo"
        

        您可以将其放入数据框或其他任何内容中。我喜欢这个答案,因为它是相对容易阅读的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-09
          • 2022-12-21
          • 2018-09-19
          • 2017-10-13
          • 2017-09-15
          • 2021-10-15
          • 1970-01-01
          相关资源
          最近更新 更多