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.silver...@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]]

______________________________________________
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.

Reply via email to