相关疑难解决方法(0)

将变量名称的向量传递给dplyr中的arrange()

我想传递arrange(){dplyr}一个变量名的向量来排序.通常我只需输入我想要的变量,但我正在尝试创建一个函数,其中排序变量可以作为函数参数输入.

df <- structure(list(var1 = c(1L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 4L, 4L
  ), var2 = structure(c(10L, 1L, 8L, 3L, 5L, 4L, 7L, 9L, 2L, 6L
  ), .Label = c("b", "c", "f", "h", "i", "o", "s", "t", "w", "x"
  ), class = "factor"), var3 = c(7L, 5L, 5L, 8L, 5L, 8L, 6L, 7L, 
  5L, 8L), var4 = structure(c(8L, 5L, 1L, 4L, 7L, 4L, 3L, 6L, 9L, 
  2L), .Label = c("b", "c", "d", "e", "f", "h", "i", …
Run Code Online (Sandbox Code Playgroud)

sorting r parameter-passing dplyr

32
推荐指数
4
解决办法
1万
查看次数

如何使用quosures将命名向量传递给dplyr :: select?

使用旧select_()函数,我可以将命名向量传递给select并立即更改位置和列名:

my_data  <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30)
my_newnames  <-  c("newbar" = "bar", "newfoo" = "foo")

move_stuff  <- function(df, newnames) {
    select_(df, .dots = newnames)
}

move_stuff(my_data,  newnames = my_newnames) )

# this is the desired output
# A tibble: 4 x 2
  newbar  newfoo
   <int>   <int>
1     10       0
2     11       1
3     12       2
4     13       3
Run Code Online (Sandbox Code Playgroud)

我尝试使用quosures和拼接做类似的事情 - 选择列效果很好,但是矢量的名称(因此同时重命名列)似乎被忽略了.以下两个都返回数据框,列中包含名称bar和foo,但不是newbar和newfoo:

move_stuff2  <- function(df, newnames) …
Run Code Online (Sandbox Code Playgroud)

r dplyr nse rlang

8
推荐指数
1
解决办法
1175
查看次数

如何使用dplyr评估具有非标准评估的构造字符串?

我已经阅读了几个关于dplyr编程的指南,我仍然对如何解决使用非标准评估(NSE)评估构造/连接字符串的问题感到困惑.我意识到有更好的方法来解决这个例子,而不是使用NSE,但想要学习如何.

t <- tibble( x_01 = c(1, 2, 3), x_02 = c(4, 5, 6))
i <- 1
Run Code Online (Sandbox Code Playgroud)

这是我想要的结果,但是想要mutate()构造变量:

t %>% mutate(d_01 = x_01 * 2)
#>   A tibble: 3 x 3
#>   x_01  x_02  d_01
#>   <dbl> <dbl> <dbl>
#> 1  1.00  4.00  2.00
#> 2  2.00  5.00  4.00
#> 3  3.00  6.00  6.00
Run Code Online (Sandbox Code Playgroud)

这是我第一次尝试使用字符串:

new <- sprintf("d_%02d", i)
var <- sprintf("x_%02d", i)
t %>% mutate(new = var * 2)
#> Error in mutate_impl(.data, dots) : …
Run Code Online (Sandbox Code Playgroud)

r dplyr nse tidyeval rlang

3
推荐指数
1
解决办法
421
查看次数

标签 统计

dplyr ×3

r ×3

nse ×2

rlang ×2

parameter-passing ×1

sorting ×1

tidyeval ×1