A simple example. I would like to create a data frame with percentages using cast / melt instead of sums.
Example.
eg <- data.frame( Time = factor(c(1,2,1,2)), A1 = c(0, 0, 1, 1), A2 = c(1, 1, 1, 1), B1 = c(0, 0, 0, 0) ) eg.m <- melt(eg,id="Time") eg.c <- cast(eg.m,Time ~ variable, sum, margins="grand_row")
In the above example, I can get the amount and amount. Instead of producing a sum, is there a way to produce a percentage in each cell, that is, the sum of the cell / gran _row? I know I can do some things here using ddply and reshape, but I wonder if there is a more elegant solution.
Here is an example of what I'm looking for:
Time A1 A2 B1 1 1 0.5 0.5 0 2 2 1.0 1.0 0
source share