当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。