简体   繁体   中英

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)

From the above example, I would like to divide columns x and a by y rowwise. The suggested method is on this page Click here But I have to use column names as an argument in across 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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM