當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R corrr rearrange 重新排列相關 DataFrame


重新排列相關 DataFrame ,將高度相關的變量更緊密地分組在一起。

用法

rearrange(x, method = "PC", absolute = TRUE)

參數

x

cor_df。請參閱correlate

method

指定排列(聚類)方法的字符串。聚類是通過 seriate 實現的,可以參考它來獲取聚類方法的完整列表。默認="PCA"。

absolute

布爾值是否應使用相關性的絕對值進行聚類。

cor_df。請參閱correlate

例子

x <- correlate(mtcars)
#> Correlation computed with
#> • Method: 'pearson'
#> • Missing treated using: 'pairwise.complete.obs'

rearrange(x) # Default settings
#> # A tibble: 11 × 12
#>    term     mpg     vs    drat      am   gear    qsec    carb     hp
#>    <chr>  <dbl>  <dbl>   <dbl>   <dbl>  <dbl>   <dbl>   <dbl>  <dbl>
#>  1 mpg   NA      0.664  0.681   0.600   0.480  0.419  -0.551  -0.776
#>  2 vs     0.664 NA      0.440   0.168   0.206  0.745  -0.570  -0.723
#>  3 drat   0.681  0.440 NA       0.713   0.700  0.0912 -0.0908 -0.449
#>  4 am     0.600  0.168  0.713  NA       0.794 -0.230   0.0575 -0.243
#>  5 gear   0.480  0.206  0.700   0.794  NA     -0.213   0.274  -0.126
#>  6 qsec   0.419  0.745  0.0912 -0.230  -0.213 NA      -0.656  -0.708
#>  7 carb  -0.551 -0.570 -0.0908  0.0575  0.274 -0.656  NA       0.750
#>  8 hp    -0.776 -0.723 -0.449  -0.243  -0.126 -0.708   0.750  NA    
#>  9 wt    -0.868 -0.555 -0.712  -0.692  -0.583 -0.175   0.428   0.659
#> 10 disp  -0.848 -0.710 -0.710  -0.591  -0.556 -0.434   0.395   0.791
#> 11 cyl   -0.852 -0.811 -0.700  -0.523  -0.493 -0.591   0.527   0.832
#> # … with 3 more variables: wt <dbl>, disp <dbl>, cyl <dbl>
#> # ℹ Use `colnames()` to see all variable names
rearrange(x, method = "HC") # Different seriation method
#> # A tibble: 11 × 12
#>    term      wt    cyl   disp     hp    carb    drat      am   gear
#>    <chr>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>  <dbl>
#>  1 wt    NA      0.782  0.888  0.659  0.428  -0.712  -0.692  -0.583
#>  2 cyl    0.782 NA      0.902  0.832  0.527  -0.700  -0.523  -0.493
#>  3 disp   0.888  0.902 NA      0.791  0.395  -0.710  -0.591  -0.556
#>  4 hp     0.659  0.832  0.791 NA      0.750  -0.449  -0.243  -0.126
#>  5 carb   0.428  0.527  0.395  0.750 NA      -0.0908  0.0575  0.274
#>  6 drat  -0.712 -0.700 -0.710 -0.449 -0.0908 NA       0.713   0.700
#>  7 am    -0.692 -0.523 -0.591 -0.243  0.0575  0.713  NA       0.794
#>  8 gear  -0.583 -0.493 -0.556 -0.126  0.274   0.700   0.794  NA    
#>  9 qsec  -0.175 -0.591 -0.434 -0.708 -0.656   0.0912 -0.230  -0.213
#> 10 mpg   -0.868 -0.852 -0.848 -0.776 -0.551   0.681   0.600   0.480
#> 11 vs    -0.555 -0.811 -0.710 -0.723 -0.570   0.440   0.168   0.206
#> # … with 3 more variables: qsec <dbl>, mpg <dbl>, vs <dbl>
#> # ℹ Use `colnames()` to see all variable names
rearrange(x, absolute = FALSE) # Not using absolute values for arranging
#> # A tibble: 11 × 12
#>    term     mpg     vs    drat      am   gear    qsec    carb     hp
#>    <chr>  <dbl>  <dbl>   <dbl>   <dbl>  <dbl>   <dbl>   <dbl>  <dbl>
#>  1 mpg   NA      0.664  0.681   0.600   0.480  0.419  -0.551  -0.776
#>  2 vs     0.664 NA      0.440   0.168   0.206  0.745  -0.570  -0.723
#>  3 drat   0.681  0.440 NA       0.713   0.700  0.0912 -0.0908 -0.449
#>  4 am     0.600  0.168  0.713  NA       0.794 -0.230   0.0575 -0.243
#>  5 gear   0.480  0.206  0.700   0.794  NA     -0.213   0.274  -0.126
#>  6 qsec   0.419  0.745  0.0912 -0.230  -0.213 NA      -0.656  -0.708
#>  7 carb  -0.551 -0.570 -0.0908  0.0575  0.274 -0.656  NA       0.750
#>  8 hp    -0.776 -0.723 -0.449  -0.243  -0.126 -0.708   0.750  NA    
#>  9 wt    -0.868 -0.555 -0.712  -0.692  -0.583 -0.175   0.428   0.659
#> 10 disp  -0.848 -0.710 -0.710  -0.591  -0.556 -0.434   0.395   0.791
#> 11 cyl   -0.852 -0.811 -0.700  -0.523  -0.493 -0.591   0.527   0.832
#> # … with 3 more variables: wt <dbl>, disp <dbl>, cyl <dbl>
#> # ℹ Use `colnames()` to see all variable names
源代碼:R/internal.R

相關用法


注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Re-arrange a correlation data frame。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。