tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.79k stars 2.12k forks source link

rank function not working properly after using mean function #7020

Closed ferreirafm closed 6 months ago

ferreirafm commented 6 months ago

Rank function is not working properly after using the mean function to build

Code is as follows:

library(tidyverse)
tib <- tibble( name = c("X", "Y", "Z"), A = c(10, 20, 30), B = c(5, 15, 25), C = c(3, 60, 23))
tib
#> # A tibble: 3 × 4
#>   name      A     B     C
#>   <chr> <dbl> <dbl> <dbl>
#> 1 X        10     5     3
#> 2 Y        20    15    60
#> 3 Z        30    25    23
tib <- tib %>% mutate(rank1 = rank(C))
tib
#> # A tibble: 3 × 5
#>   name      A     B     C rank1
#>   <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 X        10     5     3     1
#> 2 Y        20    15    60     3
#> 3 Z        30    25    23     2
tib <- tib %>% rowwise %>% mutate(Mean = mean(c_across( -any_of(c("name", "rank1")))))
tib
#> # A tibble: 3 × 6
#> # Rowwise: 
#>   name      A     B     C rank1  Mean
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 X        10     5     3     1   6  
#> 2 Y        20    15    60     3  31.7
#> 3 Z        30    25    23     2  26
tib <- tib %>% mutate(rank2 = rank(Mean))
tib
#> # A tibble: 3 × 7
#> # Rowwise: 
#>   name      A     B     C rank1  Mean rank2
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 X        10     5     3     1   6       1
#> 2 Y        20    15    60     3  31.7     1
#> 3 Z        30    25    23     2  26       1
sessionInfo()
#> R version 4.3.1 (2023-06-16)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 12 (bookworm)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: America/Sao_Paulo
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4    
#>  [5] purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1   
#>  [9] ggplot2_3.5.1   tidyverse_2.0.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.5      compiler_4.3.1    reprex_2.1.0      tidyselect_1.2.1 
#>  [5] scales_1.3.0      yaml_2.3.8        fastmap_1.1.1     R6_2.5.1         
#>  [9] generics_0.1.3    knitr_1.46        munsell_0.5.1     R.cache_0.16.0   
#> [13] tzdb_0.4.0        pillar_1.9.0      R.utils_2.12.3    rlang_1.1.3      
#> [17] utf8_1.2.4        stringi_1.8.3     xfun_0.43         fs_1.6.4         
#> [21] timechange_0.3.0  cli_3.6.2         withr_3.0.0       magrittr_2.0.3   
#> [25] digest_0.6.35     grid_4.3.1        rstudioapi_0.16.0 hms_1.1.3        
#> [29] lifecycle_1.0.4   R.methodsS3_1.8.2 R.oo_1.26.0       vctrs_0.6.5      
#> [33] evaluate_0.23     glue_1.7.0        styler_1.10.3     fansi_1.0.6      
#> [37] colorspace_2.1-0  rmarkdown_2.26    tools_4.3.1       pkgconfig_2.0.3  
#> [41] htmltools_0.5.8.1

Created on 2024-05-07 with reprex v2.1.0

joranE commented 6 months ago

You need to ungroup to turn off the rowwise effect.