[英]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
Created on 2020-08-01 by the reprex package (v0.3.0)由reprex package (v0.3.0) 于 2020 年 8 月 1 日创建
From the above example, I would like to divide columns x and a by y rowwise.从上面的示例中,我想将列 x 和 a 除以 y 行。 The suggested method is on this page Click here But I have to use column names as an argument in across function.
建议的方法在此页面上单击此处但我必须在 function 中使用列名作为参数。 Is there any way, I can use column numbers instead of their names?
有什么办法,我可以用列号代替他们的名字吗? I would appreciate if I can use the trick for all instances of across.
如果我可以将这个技巧用于所有实例,我将不胜感激。
We can replace the unquoted name with column index我们可以用列索引替换未加引号的名称
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
We can use column names我们可以使用列名
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.