- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些数据,我想用 R 中的一些汇总值正确格式化。我玩过 aggregate
和其他东西,比如 summaryBy
,但是没有一个产生我想要的。
这是数据
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")
)
我希望数据按以下方式进行总结。
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 |
-------------------+------------+----------+-----------+-----------+------------+-----------+-------------
我尝试使用聚合的方法之一如下:
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 中做我想做的事?
最佳答案
这是一种方法:
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")
将第一列转换为行名称,但是,它已被弃用。
关于r - 为 r 中的每个变量按组汇总表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246048/
我的数据库中有 4 个表。 2012,2013,2014,2015. 每一个都是这样布置的: 我想对每年的每个 StartStation 求和: StartStation 2012
您建议使用哪种方法,为什么? 创建汇总表和 . . . 1) 实时更新表格。 2) 每 15 分钟运行一次 group by 查询以更新汇总表。 3) 还有别的吗? 数据必须接近实时,不能等一个小时、
我正在尝试使用来自 python 的 pandas 的 .describe() 创建一个汇总表。 我有以下数据框: df = pd.DataFrame({'Group':['Group1', 'Gro
我在 pandas 中得到了下表: x 是 1 1 2 3 2 5 2 4 1 4 1 5 我想看看变量x的模式,所以我想看看模式是什么。 在表格中,您会看到 x=1 然后 x=2 三次,然后返回
我有一个数据库,其中有许多具有关系的表中的数据 TABLE Cars (stock) --------------------- Model colourid Doors --------
我正在尝试对列表求和,但跳过第一个偶数,然后继续添加列表的其余部分,包括其余的偶数,但我似乎不太正确。 list = [-3, -7, -1, 0, 1, 2, 3, 4, 5, 6, 7] def
我正在开发一个我需要创建和维护的项目 汇总表 出于性能原因。我相信正确的术语是 物化 View . 我有两个主要原因这样做: 非规范化 我尽可能地对表格进行了标准化。所以在某些情况下,我必须加入许多表
我有两个表,表 A 有列 token(主键)和 停机时间(INT),表 B 有列 token, status(ENUM 有 3 种状态:active, unstable, inactive ), du
我正在使用包 purrr 和 broom 来生成一系列 glm 并构建一个包含模型信息的表格,以便我可以比较它们。 当我从 purrr 调用 map 函数时,代码失败。我认为问题与 mutate 和
我是一名优秀的程序员,十分优秀!