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