要实际“做相反的事情:将行名转换为第一列”,如顶部所述,即将一列数据转换为行名,您需要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 创建