【问题标题】:How to get row index number in R?如何在R中获取行索引号?
【发布时间】:2011-01-23 03:40:34
【问题描述】:

假设我在 R 中有一个列表或数据框,我想获取行索引,我该怎么做?也就是说,我想知道某个矩阵由多少行组成。

【问题讨论】:

  • 这个问题的措辞仍然很不清楚。
  • 我认为这是因为这是两个问题:第二句话是@BenBolker 回答nrow(x) 的内容(即给出数据框的维度)。第一句话是关于获取行号,Shane 回答了(并且没有改进)as.integer(rownames(x))

标签: r dataframe


【解决方案1】:

不清楚你到底想做什么。

要引用数据框中的一行,请使用df[row,]

要获得某事物向量中的第一个位置,请使用match(item,vector),其中向量可以是数据框的列之一,例如,如果列名为 cname,则为 df$cname

编辑:

要结合这些,你会写:

df[match(item,df$cname),]

请注意,匹配项会为您提供列表中的第一项,因此如果您不是在寻找唯一的参考编号,您可能需要考虑其他内容。

【讨论】:

  • 这是对我认为@lebesgue 的另一个自我所问的不同问题的一个很好的答案。如果您键入head(df),则除行 ID 之外的所有列都有标题。所以我认为@lebesgue 正在寻找一个允许访问行 ID 的函数(row 函数)。
【解决方案2】:

我将您的问题解释为关于获取行号。

  • 如果你还没有设置行名,你可以试试as.numeric(rownames(df))。否则使用1:nrow(df) 的序列。
  • which() 函数将 TRUE/FALSE 行索引转换为行号。

【讨论】:

  • 这也是我所做的,因为 DF 行名称,即使它们是数字,也不一定对应于行索引:row(cars[34:50,])[,1]; as.integer(rownames(cars[34:50,]))。你为什么在这里使用as.numeric()?使用as.integer() 不是更有意义吗?
【解决方案3】:

如果我理解您的问题,您只是希望能够按行访问数据框(或列表)中的项目:

x = matrix( ceiling(9*runif(20)), nrow=5  )   
colnames(x) = c("col1", "col2", "col3", "col4")
df = data.frame(x)      # create a small data frame

df[1,]                  # get the first row
df[3,]                  # get the third row
df[nrow(df),]           # get the last row

lf = as.list(df)        

lf[[1]]                 # get first row
lf[[3]]                 # get third row

等等

【讨论】:

    【解决方案4】:

    参见?base::row 中的row。这给出了任何类似矩阵的对象的行索引。

    【讨论】:

      【解决方案5】:

      也许这个“匹配”的补充示例会有所帮助。

      有两个数据集:

      first_dataset <- data.frame(name = c("John", "Luke", "Simon", "Gregory", "Mary"),
                                  role = c("Audit", "HR", "Accountant", "Mechanic", "Engineer"))
      
      second_dataset <- data.frame(name = c("Mary", "Gregory", "Luke", "Simon"))
      

      如果名称列仅包含唯一跨集合值(跨整个集合) 然后您可以通过 match 返回的索引值访问其他数据集中的行

      name_mapping <- match(second_dataset$name, first_dataset$name)
      

      match 从第二个给定名称返回 first_dataset 中名称的正确行索引:5 4 2 1 此处的示例 - 按行索引(按给定名称值)从第一个数据集中访问角色

      for(i in 1:length(name_mapping)) {
          role <- as.character(first_dataset$role[name_mapping[i]])   
          second_dataset$role[i] = role
      }
      

      ===

      second dataset with new column:
           name       role
      1    Mary   Engineer
      2 Gregory   Mechanic
      3    Luke Supervisor
      4   Simon Accountant
      

      【讨论】:

        【解决方案6】:
        rownames(dataframe)
        

        这将为您提供数据帧的索引

        【讨论】:

        • 但请注意,它会将行索引(实际上是数字)作为字符向量返回,因此在许多情况下(例如绘图),您必须将其转换回数字。
        • 是的,我喜欢这个解决方案,因为它很简单,但也许as.numeric(rownames(dataframe)) 会更好。
        【解决方案7】:
        x <-  matrix(ceiling(9*runif(20)), nrow=5)
        colnames(x) <-  c("these", "are", "the", "columnes")
        df <-  data.frame(x)
        

        结果: dataframe

        which(df == "2")                       #returns rowIndexes results from the entire dataset, in this case it returns a list of 3 index numb
        

        结果:

        5 13 17

        length(which(df == "2"))               #count numb. of rows that matches the condition of ==2
        

        结果:

        3

        您也可以明智地执行此列,例如:

        which(df$columnName == c("2", "7"))     #you do the same with strings
        length(which(df$columnName == c("2", "7")))
        

        【讨论】:

          猜你喜欢
          • 2020-06-22
          • 2015-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多