如何根据R中以数字命名的列来变异tibble中的变量



我有一个tibble,其中列以数字命名(例如1(。我创建了一个函数来计算列之间的差异,但我不知道如何使用这种类型的列:

<!-- language-all: lang-r -->

library(tidyverse)
df <- tibble(`1` = c(1,2,3), `2` = c(2,4,6))
# This works
df %>%
mutate(diff = `1` - `2`) 
#> # A tibble: 3 x 3
#>     `1`   `2`  diff
#>   <dbl> <dbl> <dbl>
#> 1     1     2    -1
#> 2     2     4    -2
#> 3     3     6    -3
# But this doesn't
calc_diffs <- function(x, y){
df %>%
mutate(diff := !!x - !!y)
}
calc_diffs(1, 2)
#> # A tibble: 3 x 3
#>     `1`   `2`  diff
#>   <dbl> <dbl> <dbl>
#> 1     1     2    -1
#> 2     2     4    -1
#> 3     3     6    -1
<sup>Created on 2020-10-14 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

我们可以转换为符号并评估

calc_diffs <- function(x, y){
df %>%
mutate(diff := !! rlang::sym(x) - !!rlang::sym(y))
}

然后,我们只传递一个字符串作为参数

calc_diffs("1", "2")
# A tibble: 3 x 3
#    `1`   `2`  diff
#  <dbl> <dbl> <dbl>
#1     1     2    -1
#2     2     4    -2
#3     3     6    -3

列名是字符串。我们可以传递索引来对列进行子集设置,但这里的列名是一个以数字开头的异常名称。因此,我们可以使用paste用backreference包装它,或者只传递一个字符串,转换为symbol并计算(!!(

这行吗:

> df <- tibble(`1` = c(1,2,3), `2` = c(2,4,6))
> df
# A tibble: 3 x 2
`1`   `2`
<dbl> <dbl>
1     1     2
2     2     4
3     3     6
> calc_diffs <- function(x, y){
+   df %>%
+     mutate(diff = {{x}} - {{y}})
+ }
> calc_diffs(`1`,`2`)
# A tibble: 3 x 3
`1`   `2`  diff
<dbl> <dbl> <dbl>
1     1     2    -1
2     2     4    -2
3     3     6    -3
> 

最新更新