【问题标题】:How to get row from R data.frame如何从 R data.frame 中获取行
【发布时间】:2010-11-19 04:04:50
【问题描述】:

我有一个带有列标题的 data.frame。

如何从 data.frame 中获取特定行作为列表(将列标题作为列表的键)?

具体来说,我的data.frame是

甲乙丙 1 5 4.25 4.5 2 3.5 4 2.5 3 3.25 4 4 4 4.25 4.5 2.25 5 1.5 4.5 3

我想得到一个相当于

的行
> c(a=5, b=4.25, c=4.5)
  a   b   c 
5.0 4.25 4.5 

【问题讨论】:

    标签: r indexing dataframe


    【解决方案1】:
    x[r,]
    

    其中 r 是您感兴趣的行。试试这个,例如:

    #Add your data
    x <- structure(list(A = c(5,    3.5, 3.25, 4.25,  1.5 ), 
                        B = c(4.25, 4,   4,    4.5,   4.5 ),
                        C = c(4.5,  2.5, 4,    2.25,  3   )
                   ),
                   .Names    = c("A", "B", "C"),
                   class     = "data.frame",
                   row.names = c(NA, -5L)
         )
    
    #The vector your result should match
    y<-c(A=5, B=4.25, C=4.5)
    
    #Test that the items in the row match the vector you wanted
    x[1,]==y
    

    This page(来自this useful site)有很好的关于索引的信息。

    【讨论】:

      【解决方案2】:

      逻辑索引非常R-ish。试试:

       x[ x$A ==5 & x$B==4.25 & x$C==4.5 , ] 
      

      或者:

      subset( x, A ==5 & B==4.25 & C==4.5 )
      

      【讨论】:

        【解决方案3】:

        试试:

        > d <- data.frame(a=1:3, b=4:6, c=7:9)
        
        > d
          a b c
        1 1 4 7
        2 2 5 8
        3 3 6 9
        
        > d[1, ]
          a b c
        1 1 4 7
        
        > d[1, ]['a']
          a
        1 1
        

        【讨论】:

          【解决方案4】:

          如果您不知道行号,但知道一些值,那么您可以使用子集

          x <- structure(list(A = c(5,    3.5, 3.25, 4.25,  1.5 ), 
                              B = c(4.25, 4,   4,    4.5,   4.5 ),
                              C = c(4.5,  2.5, 4,    2.25,  3   )
                         ),
                         .Names    = c("A", "B", "C"),
                         class     = "data.frame",
                         row.names = c(NA, -5L)
               )
          
          subset(x, A ==5 & B==4.25 & C==4.5)
          

          【讨论】:

          • 你是这个意思吗?子集(x,A==5 && B==4.25 && C==4.5)
          • 不,应该是:subset(x, A ==5 &amp; B==4.25 &amp; C==4.5)
          【解决方案5】:

          10 年后 ---> 使用 tidyverse,我们可以简单地实现这一点,并从 Christopher Bottoms 借一片叶子。为了更好地掌握,请参阅slice()

          library(tidyverse)
          x <- structure(list(A = c(5,    3.5, 3.25, 4.25,  1.5 ), 
                              B = c(4.25, 4,   4,    4.5,   4.5 ),
                              C = c(4.5,  2.5, 4,    2.25,  3   )
          ),
          .Names    = c("A", "B", "C"),
          class     = "data.frame",
          row.names = c(NA, -5L)
          )
          
          x
          #>      A    B    C
          #> 1 5.00 4.25 4.50
          #> 2 3.50 4.00 2.50
          #> 3 3.25 4.00 4.00
          #> 4 4.25 4.50 2.25
          #> 5 1.50 4.50 3.00
          
          y<-c(A=5, B=4.25, C=4.5)
          y
          #>    A    B    C 
          #> 5.00 4.25 4.50
          
          #The slice() verb allows one to subset data row-wise. 
          x <- x %>% slice(1) #(n) for the nth row, or (i:n) for range i to n, (i:n()) for i to last row...
          
          x
          #>   A    B   C
          #> 1 5 4.25 4.5
          
          #Test that the items in the row match the vector you wanted
          x[1,]==y
          #>      A    B    C
          #> 1 TRUE TRUE TRUE
          

          reprex package (v0.3.0) 于 2020 年 8 月 6 日创建

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-07-16
            • 2020-09-26
            • 2016-07-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多