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.
Here's the data
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)))
but it produced in this format:
[,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"
Is it possible to do what I want in R?
Here is a way to do it using base R and
aggregate
Here's one way to do it:
You might add
%>% tibble::column_to_rownames("row")
to turn the first column into row names, however, it's deprecated.