因子水平的多变量的具体汇总统计

时间:2017-10-03 02:51:43

标签: r plyr

我试图获得mpg,价格,重量和修复记录的平均值,标准差,最小值,最大值和范围,这两个因子级别(国内和国外)在一个名为foreign的变量中。我遇到过很多例子,展示了如何在多个变量上得到一个统计量,例如如何获得一个由两个因子级别分组的变量的多个统计量。但是,我没有发现任何对我开发上面描述的表格特别有用的东西。

我尝试了很多东西,似乎ddply可能就是我应该使用的东西。我认为它应该像ddply(df,[column I want to use as factor level], mean=mean(value),...但不确定语法。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

我赞成使用$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 方法,例如:

tidyverse

...或将library(tibble) library(dplyr) mtcars %>% rownames_to_column() %>% as_tibble() %>% group_by(rowname) %>% summarise_all( funs(mean = mean, median = median, min = min, max = max, sd = sd) ) # # A tibble: 32 x 56 # rowname mpg_mean cyl_mean disp_mean hp_mean drat_mean wt_mean qsec_mean # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> # 1 AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 # 2 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 # 3 Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 # 4 Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 # 5 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 # 6 Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 # 7 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 # 8 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 # 9 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 # 10 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 summarise_if谓词

一起使用
is.numeric

...如果需要,您可以随时为函数添加参数,例如 na.rm ,例如library(dplyr) starwars %>% group_by(homeworld) %>% summarise_if( is.numeric, funs(mean = mean, median = median, min = min, max = max, sd = sd) ) # # A tibble: 49 x 16 # homeworld height_mean mass_mean birth_year_mean height_median mass_median birth_year_median height_min # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> # 1 Alderaan 176.3333 NA NA 188 NA NA 150 # 2 Aleen Minor 79.0000 15.0 NA 79 15.0 NA 79 # 3 Bespin 175.0000 79.0 37 175 79.0 37 175 # 4 Bestine IV 180.0000 110.0 NA 180 110.0 NA 180 # 5 Cato Neimoidia 191.0000 90.0 NA 191 90.0 NA 191 # 6 Cerea 198.0000 82.0 92 198 82.0 92 198 # 7 Champala 196.0000 NA NA 196 NA NA 196 # 8 Chandrila 150.0000 NA 48 150 NA 48 150 # 9 Concord Dawn 183.0000 79.0 66 183 79.0 66 183 # 10 Corellia 175.0000 78.5 25 175 78.5 25 170