使用以下示例数据帧:
a <- c(1:5)
b <- c("Cat", "Dog", "Rabbit", "Cat", "Dog")
c <- c("Dog", "Rabbit", "Cat", "Dog", "Dog")
d <- c("Rabbit", "Cat", "Dog", "Dog", "Rabbit")
e <- c("Cat", "Dog", "Dog", "Rabbit", "Cat")
f <- c("Cat", "Dog", "Dog", "Rabbit", "Cat")
df <- data.frame(a,b,c,d,e,f)
Run Code Online (Sandbox Code Playgroud)
我想调查如何重新排序列而不必输入所有列名,即, df[,c("a","d","e","f","b","c")]
我怎么说我想要列f和列f后的列?(仅引用我要移动的列或列范围?).
非常感谢您的帮助.
Sam*_*rke 38
移动特定的列的开头或data.frame的结束,使用select从dplyr包及其everything()功能.在这个例子中,我们发送到最后:
library(dplyr)
df %>%
select(-b, -c, everything())
a d e f b c
1 1 Rabbit Cat Cat Cat Dog
2 2 Cat Dog Dog Dog Rabbit
3 3 Dog Dog Dog Rabbit Cat
4 4 Dog Rabbit Rabbit Cat Dog
5 5 Rabbit Cat Cat Dog Dog
Run Code Online (Sandbox Code Playgroud)
没有否定,列将被发送到前面.
A5C*_*2T1 29
如果您只是将某些列移动到最后,您可以创建一个小帮助函数,如下所示:
movetolast <- function(data, move) {
data[c(setdiff(names(data), move), move)]
}
movetolast(df, c("b", "c"))
# a d e f b c
# 1 1 Rabbit Cat Cat Cat Dog
# 2 2 Cat Dog Dog Dog Rabbit
# 3 3 Dog Dog Dog Rabbit Cat
# 4 4 Dog Rabbit Rabbit Cat Dog
# 5 5 Rabbit Cat Cat Dog Dog
Run Code Online (Sandbox Code Playgroud)
我不建议过于习惯使用列位置,特别是从程序角度来看,因为这些位置可能会改变.
这是对上述功能的扩展解释.它允许您将列移动到第一个或最后一个位置,或者移动到另一个列之前或之后.
moveMe <- function(data, tomove, where = "last", ba = NULL) {
temp <- setdiff(names(data), tomove)
x <- switch(
where,
first = data[c(tomove, temp)],
last = data[c(temp, tomove)],
before = {
if (is.null(ba)) stop("must specify ba column")
if (length(ba) > 1) stop("ba must be a single character string")
data[append(temp, values = tomove, after = (match(ba, temp)-1))]
},
after = {
if (is.null(ba)) stop("must specify ba column")
if (length(ba) > 1) stop("ba must be a single character string")
data[append(temp, values = tomove, after = (match(ba, temp)))]
})
x
}
Run Code Online (Sandbox Code Playgroud)
尝试使用以下内容.
moveMe(df, c("b", "c"))
moveMe(df, c("b", "c"), "first")
moveMe(df, c("b", "c"), "before", "e")
moveMe(df, c("b", "c"), "after", "e")
Run Code Online (Sandbox Code Playgroud)
您需要调整它以进行一些错误检查 - 例如,如果您尝试将列"b"和"c"移动到"c之前",您(显然)会收到错误.
r.b*_*bot 12
您可以按位置引用列.例如
df <- df[ ,c(1,4:6,2:3)]
> df
a d e f b c
1 1 Rabbit Cat Cat Cat Dog
2 2 Cat Dog Dog Dog Rabbit
3 3 Dog Dog Dog Rabbit Cat
4 4 Dog Rabbit Rabbit Cat Dog
5 5 Rabbit Cat Cat Dog Dog
Run Code Online (Sandbox Code Playgroud)
包dplyr和函数dplyr::relocate是 中引入的一个新动词dplyr 1.0.0,它以高度可读的语法完全满足您的要求。
df %>% dplyr::relocate(b, c, .after = f)