r语言 - dplyr::select -包括新数据帧末尾(或开始或中间)的所有其他列



当与数据交互时,我发现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

-我很好奇是否有更好的方法来做到这一点?更好的意思是更灵活。

例如一个问题:是否有一些方法包括"所有其他"列在新的data.frame的开始或中间?(请注意,select(flights, matches("."), year, month, day, )不会产生期望的结果,因为matches(".")附加了所有列,而year, month, day被忽略,因为它们是现有列名的重复。)

Update: using dplyr::relocate()

  • 开头的选定列** *:
  • flights %>%  
      relocate(carrier, tailnum, year, month, day)
    
  • 末尾的选定列** *:
  • flights %>%  
      relocate(carrier, tailnum, year, month, day, .after = last_col()) 
    
    老回答

    如果你想**重新排序列**
  • 所有其他列**在末尾**:
  • select(flights, carrier, tailnum, year, month, day, everything()) 
    

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

    col <- c("carrier", "tailnum", "year", "month", "day")
    select(flights, one_of(col), everything()) 
    
  • 所有其他列**开头**:
  • select(flights, -one_of(col), one_of(col))
    

    如果你想添加所有的数据帧再次使用dplyr:

  • 结尾的所有数据帧:
  • bind_cols(select(flights, one_of(col)), flights)
    
  • 开头的所有数据帧:
  • bind_cols(flights, select(flights, one_of(col)))
    

    虽然不是一个非常优雅的解决方案,但它可以工作。

      select(flights, carrier, tailnum, 
    one_of(setdiff(colnames(flights),c("carrier","tailnum","year"))),year)
    

    我使用setdiff函数进行比较。由于select不接受字符串参数,我使用了one_of函数。对于select参数的许多实用函数列表,您可以参考这篇文章。

    在我看来,设置!为取一组变量的补集。

     mtcars %>% select(c(vs,am), !c(vs,am))
    

    最新更新