I have the following data.table
library(data.table) testdt <- data.table(var1=rep(c("a", "b"), e=3), p1=1:6, p2=11:16)
I need to have an average value for each var1 for each p* , p* should be in the rows and different unique values ββof var1 in the columns.
So, I am looking for this output:
variable ab 1 p1 2 5 2 p2 12 15
The easiest way to find this:
dcast(melt(testdt, id.vars = "var1", measure.vars = c("p1", "p2")), variable ~ var1, value.var = "value", fun.aggregate = median)
But I have a feeling that I missed something here (as the most suitable function), so I would like to know how directly (a unique function) to do the same.
I know that the recast package can do the trick with recast(testdt, variable~var1, fun=median, id.var="var1") , but I would like to avoid loading another package.
Edit:
I am looking for a solution, both simple and effective. This will apply to a list of ~ 40 tables with ~ 300 columns and ~ 80 rows
r data.table
Cath
source share