I am having some difficulty transposing my data correctly. I am trying to get a list of column's mean and sd where the column names are now rows. I was able to create the means and sd with the code below:
data(iris)
mydata <- do.call(data.frame, aggregate(. ~ Species, iris, function(x) c(mean = mean(x), sd = sd(x))))
Creating the Table:
<table><tbody><tr><th>Species</th><th>Sepal.Length.mean</th><th>Sepal.Length.sd</th><th>Sepal.Width.mean</th><th>Sepal.Width.sd</th><th>Petal.Length.mean</th><th>Petal.Length.sd</th><th>Petal.Width.mean</th><th>Petal.Width.sd</th></tr><tr><td>setosa</td><td>5.006</td><td>0.3524897</td><td>3.428</td><td>0.3790644</td><td>1.462</td><td>0.173664</td><td>0.246</td><td>0.1053856</td></tr><tr><td>versicolor</td><td>5.936</td><td>0.5161711</td><td>2.77</td><td>0.3137983</td><td>4.26</td><td>0.469911</td><td>1.326</td><td>0.1977527</td></tr><tr><td>virginica</td><td>6.588</td><td>0.6358796</td><td>2.974</td><td>0.3224966</td><td>5.552</td><td>0.5518947</td><td>2.026</td><td>0.27</td></tr></tbody></table>
I would like the table to look like the following:
<table><tbody><tr><th> </th><th>Setosa</th><th> </th><th>Versicolor</th><th> </th><th>Virginica</th><th> </th></tr><tr><td> </td><td>Mean</td><td>SD</td><td>Mean</td><td>SD</td><td>Mean</td><td>SD</td></tr><tr><td>Sepal.Length</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>Sepal.Width</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>Petal.Length</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>Petal.Width</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr></tbody></table>
I realize the getting the second header would most likely require the add_header_above function in kable, but before I get there I am having some difficulty structuring the dataframe to what I would like. I have been fiddling around with the cast and melt function with little luck.
Any advice would be much appreciated!
~Jack