在 dplyr 中使用列号
Using column numbers in dplyr across
library(tidyverse, warn.conflicts = TRUE)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
df <- tibble(x = 1:10, y = 11:20, z = rep(1:2, each = 5),a = runif(10))
df %>% mutate(across(c(x, a), ~ .x / y))
#> # A tibble: 10 x 4
#> x y z a
#> <dbl> <int> <int> <dbl>
#> 1 0.0909 11 1 0.0885
#> 2 0.167 12 1 0.0464
#> 3 0.231 13 1 0.0586
#> 4 0.286 14 1 0.0590
#> 5 0.333 15 1 0.0111
#> 6 0.375 16 2 0.0595
#> 7 0.412 17 2 0.0320
#> 8 0.444 18 2 0.0311
#> 9 0.474 19 2 0.0386
#> 10 0.5 20 2 0.0236
由 reprex package (v0.3.0)
于 2020-08-01 创建
在上面的例子中,我想将列 x 和 a 按 y 行划分。建议的方法在此页面上 Click here 但我必须使用列名作为跨函数中的参数。有什么办法,我可以使用列号而不是它们的名称?如果我可以对跨的所有实例使用该技巧,我将不胜感激。
我们可以用列索引替换未加引号的名称
library(dplyr)
df %>%
mutate(across(c(1, 4), ~ .x / y))
# A tibble: 10 x 4
# x y z a
# <dbl> <int> <int> <dbl>
# 1 0.0909 11 1 0.0470
# 2 0.167 12 1 0.000267
# 3 0.231 13 1 0.0453
# 4 0.286 14 1 0.0327
# 5 0.333 15 1 0.0382
# 6 0.375 16 2 0.0453
# 7 0.412 17 2 0.0105
# 8 0.444 18 2 0.0329
# 9 0.474 19 2 0.0396
#10 0.5 20 2 0.0249
我们可以使用列名
df2 <- df %>%
rowwise() %>%
mutate(across(c(x,a), ~ .x/y))
df2
# A tibble: 10 x 4
# Rowwise:
# x y z a
# <dbl> <int> <int> <dbl>
# 1 0.0909 11 1 0.0889
# 2 0.167 12 1 0.0751
# 3 0.231 13 1 0.0634
# 4 0.286 14 1 0.0626
# 5 0.333 15 1 0.0122
# 6 0.375 16 2 0.0147
# 7 0.412 17 2 0.00868
# 8 0.444 18 2 0.0000776
# 9 0.474 19 2 0.0349
# 10 0.5 20 2 0.00526
library(tidyverse, warn.conflicts = TRUE)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
df <- tibble(x = 1:10, y = 11:20, z = rep(1:2, each = 5),a = runif(10))
df %>% mutate(across(c(x, a), ~ .x / y))
#> # A tibble: 10 x 4
#> x y z a
#> <dbl> <int> <int> <dbl>
#> 1 0.0909 11 1 0.0885
#> 2 0.167 12 1 0.0464
#> 3 0.231 13 1 0.0586
#> 4 0.286 14 1 0.0590
#> 5 0.333 15 1 0.0111
#> 6 0.375 16 2 0.0595
#> 7 0.412 17 2 0.0320
#> 8 0.444 18 2 0.0311
#> 9 0.474 19 2 0.0386
#> 10 0.5 20 2 0.0236
由 reprex package (v0.3.0)
于 2020-08-01 创建在上面的例子中,我想将列 x 和 a 按 y 行划分。建议的方法在此页面上 Click here 但我必须使用列名作为跨函数中的参数。有什么办法,我可以使用列号而不是它们的名称?如果我可以对跨的所有实例使用该技巧,我将不胜感激。
我们可以用列索引替换未加引号的名称
library(dplyr)
df %>%
mutate(across(c(1, 4), ~ .x / y))
# A tibble: 10 x 4
# x y z a
# <dbl> <int> <int> <dbl>
# 1 0.0909 11 1 0.0470
# 2 0.167 12 1 0.000267
# 3 0.231 13 1 0.0453
# 4 0.286 14 1 0.0327
# 5 0.333 15 1 0.0382
# 6 0.375 16 2 0.0453
# 7 0.412 17 2 0.0105
# 8 0.444 18 2 0.0329
# 9 0.474 19 2 0.0396
#10 0.5 20 2 0.0249
我们可以使用列名
df2 <- df %>%
rowwise() %>%
mutate(across(c(x,a), ~ .x/y))
df2
# A tibble: 10 x 4
# Rowwise:
# x y z a
# <dbl> <int> <int> <dbl>
# 1 0.0909 11 1 0.0889
# 2 0.167 12 1 0.0751
# 3 0.231 13 1 0.0634
# 4 0.286 14 1 0.0626
# 5 0.333 15 1 0.0122
# 6 0.375 16 2 0.0147
# 7 0.412 17 2 0.00868
# 8 0.444 18 2 0.0000776
# 9 0.474 19 2 0.0349
# 10 0.5 20 2 0.00526