dplyr:取消选择由给出的列

dplyr: deselecting columns given by

如何取消select 列在自写函数的... 参数中给出。 (我还需要在另一点 select 列,因此仅在 ... 中指定带有 - 的列并不能解决我的问题。)

感谢任何解决方案,select-帮助者,操纵quosures或表达式,...

# very simple example data
test <- data.frame(a=1:3, b=1:3, c=1:3)

# function skeleton
testfun <- function(x, ...){
  y <- select(x, ...)
  z <- select(x, -...) # does of course not work like this
  return(list(y, z))   # just as an example
}

# calling the function to select different columns
testfun(test, a)
testfun(test, a, b)

那个呢?

testfun <- function(x, ...){
  y <- select(x, ...)
  z <- x[, !names(x) %in% names(y)]
  return(list(y, z))
}

你可以试试:

testfun <- function(x, y, z){
  y1 <- select(x, y)
  z1 <- select(x, -one_of(z)) 
  return(list(y1, z1)) 
}
testfun(test, "a", "b")
[[1]]
  a
1 1
2 2
3 3

[[2]]
  a c
1 1 1
2 2 2
3 3 3

您还可以使用 c

指定更多变量
testfun(test, c("a", "c"), c("b", "a"))

您可以将此技巧与 purrr::modify_at

一起使用
library(purrr)
testfun <- function(x, ...){
  y <- select(x, ...)
  z <- modify_at(x,c(...),~NULL)
  return(list(y, z))   # just as an example
}

testfun(test,"a")
# [[1]]
#   a
# 1 1
# 2 2
# 3 3
# 
# [[2]]
#   b c
# 1 1 1
# 2 2 2
# 3 3 3

这些最简单的解决方案是 select 正列,然后比较名称以确定要删除的列,如 .

要直接在点上工作,

  1. 我们会将它们捕获在一个问题列表中 (quos)。
  2. 取消引号并将点与 UQS 拼接为正 select 离子。
  3. c() 内部做同样的事情,这样我们就有了一个 selection 的矢量。
  4. 否定那个向量做负select离子。

这是 (3) 和 (4) 描述的转换。

library(dplyr)
dots <- quos(a, b)
quos(-c(UQS(dots)))
#> [[1]]
#> <quosure: frame>
#> ~-c(~a, ~b)
#> 
#> attr(,"class")
#> [1] "quosures"

那么完整的解决方案就是

test <- data.frame(a = 1:3, b = 1:3, c = 1:3)

# function skeleton
testfun <- function(x, ...) {
  dots <- quos(...)
  y <- select(x, UQS(dots))
  z <- select(x, -c(UQS(dots)))
  return(list(y, z))   
}

testfun(test, a)
#> [[1]]
#>   a
#> 1 1
#> 2 2
#> 3 3
#> 
#> [[2]]
#>   b c
#> 1 1 1
#> 2 2 2
#> 3 3 3

testfun(test, a, b)
#> [[1]]
#>   a b
#> 1 1 1
#> 2 2 2
#> 3 3 3
#> 
#> [[2]]
#>   c
#> 1 1
#> 2 2
#> 3 3

测试 selection helpers。

testfun(test, starts_with("b"), one_of("c"))
#> [[1]]
#>   b c
#> 1 1 1
#> 2 2 2
#> 3 3 3
#> 
#> [[2]]
#>   a
#> 1 1
#> 2 2
#> 3 3