Sum of Probabilities in a matrix...
Let's make it a data frame instead:
# Read the data from your post into a data frame named d:
d <- read.table(textConnection("
0.98 2
0.2 1
0.01 2
0.5 1
0.6 6"))
closeAllConnections()
# Use the ave() function and append the result to d:
d$sumprob <- with(d, ave(V1, V2, FUN = sum))
d
V1 V2 sumprob 1 0.98 2 0.99 2 0.20 1 0.70 3 0.01 2 0.99 4 0.50 1 0.70 5 0.60 6 0.60 HTH, Dennis
On Sat, Oct 1, 2011 at 6:06 PM, Jim Silverton <jim.silverton at gmail.com> wrote:
Hi all, I have 2 columns in a mtrix, one of which is a column of probabilities and the other is simply a vector of integers. I want to sum all the probabilities with the same integer value and put it in a new column. For example, If my matrix is: 0.98 ? 2 0.2 ? ? 1 0.01 ? 2 0.5 ? ? 1 0.6 ? ? 6 Then I should get: 0.98 ? 2 ? ?0.99 0.2 ? ? 1 ? ?0.70 0.01 ? 2 ? ?0.99 0.5 ? ? 1 ? ?0.70 0.6 ? ? 6 ? ?0.60 Any help is greatly appreciated. -- Thanks, Jim. ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.