【问题标题】:How to pull the column indices when matching the rows of a dataframe and a vector匹配数据框和向量的行时如何提取列索引
【发布时间】:2020-04-03 18:21:03
【问题描述】:

假设我有一个这样的字母数据框:

  X1 X2 X3
1  G  A  C
2  G  T  C
3  G  T  C
4  A  T  G
5  A  C  G

还有一个像这样的向量: ref <- c("A", "C", "C", "A", "G")

逐行,我如何提取与向量匹配的数据帧的列索引?

所以答案应该是一个数字向量,如下所示: 2, 3, 3, 1, 3

【问题讨论】:

    标签: r dataframe vector indexing


    【解决方案1】:

    我们可以使用

    max.col(df1 == ref)
    #[1] 2 3 3 1 3
    

    数据

    df1 <- structure(list(X1 = c("G", "G", "G", "A", "A"), X2 = c("A", "T", 
    "T", "T", "C"), X3 = c("C", "C", "C", "G", "G")), class = "data.frame",
    row.names = c("1", 
    "2", "3", "4", "5"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 2017-07-05
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多