Hi all, I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) I want to return a vector with the corresponding probabilities based on the amount of times the numbers occurred. For example, I should get the following vector for xm: prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) Any help greatly appreciated. -- Thanks, Jim. [[alternative HTML version deleted]]
Try this: prob.xm <- (table(xm)/length(xm))[match(xm, sort(unique(xm)))] Jean Jim Silverton wrote on 08/24/2011 02:31:05 PM:> Hi all, > I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) > > I want to return a vector with the corresponding probabilities based onthe> amount of times the numbers occurred. For example, I should get the > following vector for xm: > prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) > Any help greatly appreciated. > > -- > Thanks, > Jim.[[alternative HTML version deleted]]
If your numbers are all positive integers, this should work: (tabulate(xm)[xm])/length(xm) it can be put into a function for ease of use: probVec <- function(x) {(tabulate(x)[x])/length(x)} You'll have some trouble if you have non-positive integers or non-integers. Let me know if you need to handle that case: it's not much harder (just a transform in and out of integers). Hope this helps, Michael On Wed, Aug 24, 2011 at 3:31 PM, Jim Silverton <jim.silverton@gmail.com>wrote:> Hi all, > I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) > > I want to return a vector with the corresponding probabilities based on the > amount of times the numbers occurred. For example, I should get the > following vector for xm: > prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) > Any help greatly appreciated. > > -- > Thanks, > Jim. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On Aug 24, 2011, at 3:31 PM, Jim Silverton wrote:> Hi all, > I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) > > I want to return a vector with the corresponding probabilities based > on the > amount of times the numbers occurred. For example, I should get the > following vector for xm: > prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9)?prop.table Usage (with table) > prob.xm <- round( prop.table(table(xm)), digits=3) > prob.xm xm 1 2 3 4 5 6 0.111 0.111 0.111 0.111 0.333 0.222 -- David Winsemius, MD West Hartford, CT
H all, I have a 6 x 3 matrix. The last column is simply the sum of of the first two rows. x1 x2 x3 1 2 3 2 1 3 1 0 1 2 2 4 2 1 3 2 1 3 0 0 0 I want to create a column of probabilities p, such that for each row, I want to find the probability of x1=k and x3 = n divided by the probability that x3 = n. So I should end up with a column of probabilities: 1/4, 3/4, 1/7, 1/7, 3/4, 3/4, 1/7 -- Thanks, Jim. [[alternative HTML version deleted]]