按组汇总表中r中每个变量的数据 [英] Summarizing data in table by group for each variable in r

查看:91
本文介绍了按组汇总表中r中每个变量的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据想要用R中的一些汇总值正确格式化.我玩过aggregate和其他内容,例如summaryBy,但没有一个产生我想要的.

I have some data that I'd like to properly format with some summary values in R. I've played with aggregate and other things such as summaryBy, but none produced what I wanted to.

这是数据

data <- data.frame(id = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48),
                   x1 = c(0.2846,0.3741,0.4208,0.3756,0.3476,0.3664,0.2852,0.3537,0.3116,0.3124,0.364,0.3934,0.3456,0.3034,0.3139,0.2766,0.3034,0.3159,0.3648,0.4046,0.3961,0.3451,0.2059,0.3184,0.2481,0.3503,0.331,0.3166,0.3203,0.1868,0.245,0.1625,0.2227,0.196,0.1697,0.2064,0.1369,0.1938,0.1498,0.1315,0.1523,0.2151,0.168,0.1427,0.3083,0.301,0.2328,0.2747),
                   x2 = c(-0.4364,-0.5262,-0.5338,-0.5037,-0.4758,-0.5003,-0.4359,-0.5002,-0.4027,-0.424,-0.4811,-0.5492,-0.3846,-0.3899,-0.4473,-0.3688,-0.3946,-0.4112,-0.4833,-0.4909,-0.4865,-0.368,0.295,-0.3221,-0.2482,-0.5424,-0.5021,-0.4453,-0.3952,0.3915,0.4472,0.364,0.436,0.3877,0.4077,0.2737,0.3104,0.3514,0.3256,0.287,0.3126,0.3648,-0.2596,-0.1913,-0.3656,-0.4598,-0.3198,-0.3685),
                   x3 = c(0.6043,0.5141,0.4638,0.486,0.3691,0.4104,0.426,0.3846,0.3191,0.4347,0.5842,0.4638,0.4418,0.523,0.5009,0.4568,0.5105,0.5421,0.4857,0.4063,0.391,0.4114,0.5189,0.5248,0.4942,0.2855,0.6107,0.4712,0.2009,0.4632,0.4457,0.3914,0.4547,0.4801,0.4873,0.5501,0.4442,0.4458,0.4651,0.5748,0.5231,0.4869,0.1769,0.099,0.5013,0.4543,0.4601,0.4396),
                   x4 = c(0.4895,0.6991,0.6566,0.6106,0.6976,0.6883,0.6533,0.6951,0.6852,0.5062,0.5682,0.6172,0.5073,0.6514,0.577,0.5228,0.6571,0.6132,0.4893,0.7904,0.6519,0.6582,0.6919,0.6011,0.6145,0.5943,0.4608,0.5997,0.4431,0.4082,0.5641,0.4535,0.5448,0.4632,0.4237,0.6187,0.4115,0.4995,0.4504,0.4103,0.4511,0.527,0.3654,0.2537,0.6317,0.478,0.5915,0.5283),
                   trt = c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C","C","C","C","D","D","D","D","D","D")
                   )

我希望数据可以通过以下方式进行汇总.

And I'd like the data to summarize in the following way.

            A               |           B          |           C            |           D   
-------------------+------------+----------+-----------+-----------+------------+-----------+-------------
|       Mean       | Std.Dev    | Mean     | Std.Dev   | Mean      | Std.Dev    | Mean      |  Std.Dev   |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X1 |  0.3456     | 0.04104    |0.3207333 | 0.0514311 | 0.1821923 | 0.0350107  | 0.2379167 | 0.06966645 |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X2 |  -0.4674143 | 0.05489628 |-0.37406  | 0.2003379 | 0.3584308 | 0.05489583 | -0.3274333| 0.0936547  |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X3 |  0.4589214  | 0.07952784 |0.45406   | 0.1036369 | 0.4778769 | 0.04866813 | 0.3552    | 0.1713025  |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X4 |  0.6232571  | 0.0762495  |0.5976867 | 0.0914621 | 0.4789231 | 0.06686731 | 0.4747667 | 0.1428023  |
-------------------+------------+----------+-----------+-----------+------------+-----------+-------------

我尝试使用聚合的方法之一是:

One of the ways that I tried doing using aggregate is the following:

library(dplyr)
t(data[,2:5] %>% group_by(data$trt) %>% summarise_each(funs(mean, sd)))

但它以以下格式产生:

         [,1]         [,2]         [,3]         [,4]        
data$trt "A"          "B"          "C"          "D"         
x1_mean  "0.3456000"  "0.3207333"  "0.1821923"  "0.2379167" 
x2_mean  "-0.4674143" "-0.3740600" " 0.3584308" "-0.3274333"
x3_mean  "0.4589214"  "0.4540600"  "0.4778769"  "0.3552000" 
x4_mean  "0.6232571"  "0.5976867"  "0.4789231"  "0.4747667" 
x1_sd    "0.04104517" "0.05143110" "0.03501070" "0.06966645"
x2_sd    "0.05489628" "0.20033792" "0.05489583" "0.09365470"
x3_sd    "0.07952784" "0.10363689" "0.04866813" "0.17130249"
x4_sd    "0.07624950" "0.09146218" "0.06686731" "0.14280235"

是否可以在R中做我想做的事?

Is it possible to do what I want in R?

推荐答案

这里是一种方法:

data %>% 
  select(-id) %>% 
  gather(row, val, -trt) %>% 
  group_by(trt, row) %>% 
  summarise_all(funs(Mean=mean, `Std.Dev`=sd)) %>% 
  gather(col, val, Mean, `Std.Dev`) %>% 
  unite("col", trt, col) %>% 
  spread(col, val) 
# # A tibble: 4 x 9
#   row   A_Mean A_Std.Dev B_Mean B_Std.Dev C_Mean C_Std.Dev D_Mean D_Std.Dev
# * <chr>  <dbl>     <dbl>  <dbl>     <dbl>  <dbl>     <dbl>  <dbl>     <dbl>
# 1 x1     0.346    0.0410  0.321    0.0514  0.182    0.0350  0.238    0.0697
# 2 x2    -0.467    0.0549 -0.374    0.200   0.358    0.0549 -0.327    0.0937
# 3 x3     0.459    0.0795  0.454    0.104   0.478    0.0487  0.355    0.171 
# 4 x4     0.623    0.0762  0.598    0.0915  0.479    0.0669  0.475    0.143 

您可以添加%>% tibble::column_to_rownames("row")以将第一列转换为行名,但是不建议使用.

You might add %>% tibble::column_to_rownames("row") to turn the first column into row names, however, it's deprecated.

这篇关于按组汇总表中r中每个变量的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆