dply:在R中按字母顺序排列

Zac*_*ach 31 r dataframe dplyr

如果我有一个大的DF(数百和数百)列,其中不同的col_names按字母顺序随机分布:

df.x <- data.frame(2:11, 1:10, rnorm(10))
colnames(df.x) <- c("ID", "string", "delta")
Run Code Online (Sandbox Code Playgroud)

如何按字母顺序按col_name排序所有数据(垂直)?

基本上,我有数百个CSV(sep ="|")文本文件,我需要将它们的列读入单个df,按字母顺序排列这些列,然后使用其他一些dplyf函数来获得最终结果.除了如何按字母顺序排列列之外,我已经解决了所有问题.我不想按字母排序(上下)列,而是col_names的实际垂直方向及其相应的数据.类似于在Excel中剪切和粘贴整列数据.

例如,我回顾了这种方法,但这是按字母顺序排序的行,这不是我想要做的.

如何按列对数据框进行排序?

谢谢!

Kou*_*ndy 29

试试这个

df %>% select(noquote(order(colnames(df))))
Run Code Online (Sandbox Code Playgroud)

要不就

df[,order(colnames(df))]
Run Code Online (Sandbox Code Playgroud)

  • 或者只是`df.x%>%select(order(colnames(.)))` (18认同)

Ste*_*cke 19

在dplyr中执行此操作的另一种方法是:

iris %>% 
  select(sort(current_vars()))
Run Code Online (Sandbox Code Playgroud)

current_vars()返回列名,使它们可以排序,并select()采用列名的向量.

  • 2019 年 12 月更新。“current_vars()”已被弃用,取而代之的是“tidyselect::peek_vars()”。上面的代码适用于这种替换。`选择(排序(tidyselect::peek_vars()))` (13认同)
  • 在这种形式,我收到错误消息"错误:变量上下文未设置".`current_vars()`可能会被弃用?用`everything()`替换`current_vars()`对我来说运行正常. (7认同)
  • 您还可以使用新的“relocate()”动词。```iris %&gt;% 重新定位(排序(current_vars))``` (2认同)

MS *_*nds 11

tidyselect对于使用最近的 tidyverse (更具体地说,包)进行部分排序,请使用peek_vars()

library(dplyr)

starwars
#> # A tibble: 87 x 14
#>    name               height  mass hair_color    skin_color  eye_color birth_year
#>    <chr>               <int> <dbl> <chr>         <chr>       <chr>          <dbl>
#>  1 Luke Skywalker        172    77 blond         fair        blue            19.0
#>  2 C-3PO                 167    75 NA            gold        yellow         112.0
#>  3 R2-D2                  96    32 NA            white, blue red             33.0
#>  4 Darth Vader           202   136 none          white       yellow          41.9
#>  5 Leia Organa           150    49 brown         light       brown           19.0
#>  6 Owen Lars             178   120 brown, grey   light       blue            52.0
#>  7 Beru Whitesun lars    165    75 brown         light       blue            47.0
#>  8 R5-D4                  97    32 NA            white, red  red               NA
#>  9 Biggs Darklighter     183    84 black         light       brown           24.0
#> 10 Obi-Wan Kenobi        182    77 auburn, white fair        blue-gray       57.0
#> # ... with 77 more rows, and 7 more variables: sex <chr>, gender <chr>,
#> #   homeworld <chr>, species <chr>, films <list>, vehicles <list>, starships <list>

starwars %>% select(name, mass, sort(tidyselect::peek_vars()))
#> # A tibble: 87 x 14
#>    name                mass birth_year eye_color films gender    hair_color    height
#>    <chr>              <dbl>      <dbl> <chr>     <lis> <chr>     <chr>          <int>
#>  1 Luke Skywalker        77       19.0 blue      <chr> masculine blond            172
#>  2 C-3PO                 75      112.0 yellow    <chr> masculine NA               167
#>  3 R2-D2                 32       33.0 red       <chr> masculine NA                96
#>  4 Darth Vader          136       41.9 yellow    <chr> masculine none             202
#>  5 Leia Organa           49       19.0 brown     <chr> feminine  brown            150
#>  6 Owen Lars            120       52.0 blue      <chr> masculine brown, grey      178
#>  7 Beru Whitesun lars    75       47.0 blue      <chr> feminine  brown            165
#>  8 R5-D4                 32         NA red       <chr> masculine NA                97
#>  9 Biggs Darklighter     84       24.0 brown     <chr> masculine black            183
#> 10 Obi-Wan Kenobi        77       57.0 blue-gray <chr> masculine auburn, white    182
#> # ... with 77 more rows, and 6 more variables: homeworld <chr>, sex <chr>,
#> #   skin_color <chr>, species <chr>, starships <list>, vehicles <list>
Run Code Online (Sandbox Code Playgroud)


HBa*_*Bat 7

如果特定列(或多列)必须是第一个(或最后一个),但其余的已排序,您可以:

mtcars %>% tibble %>% 
  select("hp", sort(colnames(.)))
Run Code Online (Sandbox Code Playgroud)