dplyr :: select - 包括新数据框末尾(或开头或中间)的所有其他列

Eco*_*tis 17 r dplyr

在与数据交互时,我发现dplyr库的select()函数是组织数据框列的一种很好的方法.

一个很好的用途,如果我碰巧使用具有许多列的df,我经常会发现自己将两个变量放在一起以便于比较.这样做时,我需要在之前或之后附加所有其他列.我发现这个matches(".")功能是一种非常方便的方法.

例如:

library(nycflights13)
library(dplyr)

# just have the five columns:
select(flights, carrier, tailnum, year, month, day) 

# new order for all column:
select(flights, carrier, tailnum, year, month, day, matches(".")) 
# matches(".")  attached all other columns to end of new data frame
Run Code Online (Sandbox Code Playgroud)

问题 - 如果有更好的方法,我很好奇吗?更灵活的意义上更好.

例如,有一个问题:是否有某种方法可以在新data.frame的开头或中间包含"所有其他"列?(请注意,由于它们是现有列名称的重复,select(flights, matches("."), year, month, day, )因此不会产生所需的结果,因为它们是matches(".")附加的所有列,year, month, day因此它们会被忽略.

mpa*_*nco 36

如果要重新排序列

  • 其他所有列在最后:
  • select(flights, carrier, tailnum, year, month, day, everything()) 
    
    Run Code Online (Sandbox Code Playgroud)

    或者分两步,选择字符向量中提供的变量,one_of("x", "y", "z"):

    col <- c("carrier", "tailnum", "year", "month", "day")
    select(flights, one_of(col), everything()) 
    
    Run Code Online (Sandbox Code Playgroud)

  • 开头的所有其他列:
  • select(flights, -one_of(col), one_of(col))
    
    Run Code Online (Sandbox Code Playgroud)

    如果要再次使用以下命令添加所有数据框dplyr:

  • 最后的所有数据框:
  • bind_cols(select(flights, one_of(col)), flights)
    
    Run Code Online (Sandbox Code Playgroud)

  • 开头的所有数据框:
  • bind_cols(flights, select(flights, one_of(col)))
    
    Run Code Online (Sandbox Code Playgroud)