【问题标题】:How to add a row names to a data frame in a magrittr chain如何将行名称添加到 magrittr 链中的数据框
【发布时间】:2016-07-01 03:37:01
【问题描述】:

我想做相反的事情: Convert row names into first column

管道链中的某处我想将行名称添加到数据框中,例如,我想使用管道执行以下操作:

rownames(mtcars) <- as.character(1:nrow(mtcars))

所以它看起来像:

library(magrittr)
mtcars <-
    mtcars %>%
    ...???

请注意,@akrun 的回答表明,如果管道与将数据帧强制转换为 tbl_df 的函数一起使用,行名将丢失。

【问题讨论】:

    标签: r magrittr


    【解决方案1】:

    你可以使用row.names&lt;-:

    mtcars <- mtcars %>% `row.names<-`(as.character(1:nrow(mtcars)))
    

    应该可以。作为演示:

    df <- data.frame(x = 1:5, y = 2:6)
    df <- df %>% `row.names<-`(letters[1:5])
    df
    
    #   x y
    # a 1 2
    # b 2 3
    # c 3 4
    # d 4 5
    # e 5 6
    

    【讨论】:

      【解决方案2】:

      其他可能性是使用来自magrittr 库的set_rownames 别名。

      mtcars <- 
        mtcars %>%
        set_rownames(as.character(1:nrow(mtcars)))
      

      【讨论】:

        【解决方案3】:

        tbl_df 将其更改为行号。因此,我们不需要在更改行名方面做任何额外的工作。

        library(dplyr)
        tbl_df(mtcars)
        

        如果我们使用data.table,这同样适用

        as.data.table(mtcars)
        

        正如 OP 评论关于将名称更改为行序列以外的其他内容,如果我们使用其他帖子中显示的相同分配

         mtcars %>%
             `row.names<-`(c(letters, LETTERS)[1:32]) %>%
              group_by(gear) %>%
              slice(1)
        #     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
        #  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
        #1  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
        #2  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
        #3  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
        

        如我们所见,行名再次更改为顺序。因此,如果我们将行名更改为其他名称并进行 dplyr 链操作,则之前的更改毫无价值。

        【讨论】:

        • 呃,对不起,名字向量只是一个例子,并不代表……这只是我能想到的最简单的字符串。
        • 我只是关注您在帖子中显示的内容。 tbl_df 或 data.table 不能有任何其他行名
        【解决方案4】:

        要实际“做相反的事情:将行名转换为第一列”,如顶部所述,即将一列数据转换为行名,您需要tibble::column_to_rownames(),它非常适合管道。 (我知道您的示例指定了其他内容,即将一系列数字转换为行名,您应该使用其他答案)

        library(tidyverse)
        starwars %>% column_to_rownames("name") %>% head()
        #>                height mass  hair_color  skin_color eye_color birth_year
        #> Luke Skywalker    172   77       blond        fair      blue       19.0
        #> C-3PO             167   75        <NA>        gold    yellow      112.0
        #> R2-D2              96   32        <NA> white, blue       red       33.0
        #> Darth Vader       202  136        none       white    yellow       41.9
        #> Leia Organa       150   49       brown       light     brown       19.0
        #> Owen Lars         178  120 brown, grey       light      blue       52.0
        #>                gender homeworld species
        #> Luke Skywalker   male  Tatooine   Human
        #> C-3PO            <NA>  Tatooine   Droid
        #> R2-D2            <NA>     Naboo   Droid
        #> Darth Vader      male  Tatooine   Human
        #> Leia Organa    female  Alderaan   Human
        #> Owen Lars        male  Tatooine   Human
        #>                                                                                                                                                    films
        #> Luke Skywalker                                           Revenge of the Sith, Return of the Jedi, The Empire Strikes Back, A New Hope, The Force Awakens
        #> C-3PO                             Attack of the Clones, The Phantom Menace, Revenge of the Sith, Return of the Jedi, The Empire Strikes Back, A New Hope
        #> R2-D2          Attack of the Clones, The Phantom Menace, Revenge of the Sith, Return of the Jedi, The Empire Strikes Back, A New Hope, The Force Awakens
        #> Darth Vader                                                                 Revenge of the Sith, Return of the Jedi, The Empire Strikes Back, A New Hope
        #> Leia Organa                                              Revenge of the Sith, Return of the Jedi, The Empire Strikes Back, A New Hope, The Force Awakens
        #> Owen Lars                                                                                          Attack of the Clones, Revenge of the Sith, A New Hope
        #>                                          vehicles                starships
        #> Luke Skywalker Snowspeeder, Imperial Speeder Bike X-wing, Imperial shuttle
        #> C-3PO                                                                     
        #> R2-D2                                                                     
        #> Darth Vader                                                TIE Advanced x1
        #> Leia Organa                 Imperial Speeder Bike                         
        #> Owen Lars
        

        reprex package (v0.2.1) 于 2019-03-18 创建

        【讨论】:

          猜你喜欢
          • 2021-09-19
          • 1970-01-01
          • 1970-01-01
          • 2022-09-28
          • 1970-01-01
          • 2017-03-29
          • 2018-09-07
          • 1970-01-01
          • 2019-10-31
          相关资源
          最近更新 更多