首页 文章

使用带有命名向量的汇总

提问于
浏览
2

我正在尝试使用汇总,其中汇总的向量具有名称 . 汇总函数将这些名称复制到输出,但现在长度错误 . 当我尝试格式化生成的摘要时,names属性的不正确长度会引发错误 .

在我真正关心的例子中,带有名称的向量是来自glm模型的拟合值 . 我不想要这些名字,但它们是免费的 . 下面的玩具示例使用mtcars .

``` r

library(tidyverse)
#> -- Attaching packages -------------------------------------------------------------------------------- tidyverse 1.2.1 --
#> v ggplot2 2.2.1     v purrr   0.2.4
#> v tibble  1.4.1     v dplyr   0.7.4
#> v tidyr   0.7.2     v stringr 1.2.0
#> v readr   1.1.1     v forcats 0.2.0
#> -- Conflicts ----------------------------------------------------------------------------------- tidyverse_conflicts() --
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag()    masks stats::lag()
# create a copy of mtcars, with the original mpg and a copy with names
namedmpg <- mtcars$mpg
attr(namedmpg, "names") <- row.names(mtcars)
MTCARS <- bind_cols(mtcars, namedmpg = namedmpg)

当我使用原始的mpg变量时,我得到了我想要的 .

goodframe <- summarize(group_by(MTCARS, cyl), meanmpg = mean(mpg))
goodframe # gives exactly what we want
#> # A tibble: 3 x 2
#>     cyl meanmpg
#>   <dbl>   <dbl>
#> 1  4.00    26.7
#> 2  6.00    19.7
#> 3  8.00    15.1
format(goodframe$meanmpg) # formats fine
#> [1] "26.66364" "19.74286" "15.10000"

现在使用命名的mpg代替 .

badframe <- summarize(group_by(MTCARS, cyl), meanmpg = mean(namedmpg))
badframe # seems to have what we want
#> # A tibble: 3 x 2
#>     cyl meanmpg
#>   <dbl>   <dbl>
#> 1  4.00    26.7
#> 2  6.00    19.7
#> 3  8.00    15.1
format(badframe$meanmpg) # throws an error
#> Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : 'names' attribute [32] must be the same length as the vector [3]
names(badframe$meanmpg) # the original names, which are now too many
#>  [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"         
#>  [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
#>  [7] "Duster 360"          "Merc 240D"           "Merc 230"           
#> [10] "Merc 280"            "Merc 280C"           "Merc 450SE"         
#> [13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
#> [16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"           
#> [19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"      
#> [22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
#> [25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"      
#> [28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
#> [31] "Maserati Bora"       "Volvo 142E"

所以我必须做一些事情来获得可用于进一步处理的表格中的摘要 . 这是一个黑客 .

format(badframe$meanmpg + 0) # a real hack, but seems to work

当然,总有一种老式的方式

stillgood <- with(MTCARS, aggregate(namedmpg, list(cyl), mean))
stillgood
#>   Group.1        x
#> 1       4 26.66364
#> 2       6 19.74286
#> 3       8 15.10000
format(stillgood$x) # works
#> [1] "26.66364" "19.74286" "15.10000"

    ```

我不确定情况是错误还是功能 . 恕我直言,它应该以任何方式记录 .

除了上面的黑客攻击,我还可以在总结之前删除这些名字 . 但有一天我可能会想要它们 .

有没有更好的办法?

1 回答

  • 2

    如果可以接受,我们可以用 unname 包装

    format(unname(badframe$meanmpg) )
    #[1] "26.66364" "19.74286" "15.10000"
    

相关问题