[R] Assigning factors probabilistically based on the value of another variable.

Economics Guy economics.guy at gmail.com
Sat Mar 27 23:15:07 CET 2010


I am revising a program that I wrote when I was very new at R
(2007ish), and while I have been able to write very nice and fast code
for almost all of it, there is one issue that I cannot seem to do it
in less than 40 ugly and computationally expensive lines.

I have a data frame that contains one variable:

theFrame <- data.frame(theValues=runif(150,-10,10))

I would like to write a function that would assign each of these
values a factor, and I need it to meet several criteria:

(1) There are 15 factors.
(2) I need there to be exactly 10 elements assigned to each factor.

Now here is the tricky part:

(3) I would like to assign the factor probabilistically. The lower
theValue is for a row, the lower factor I would like it to receive. So
values close to -10 should have a really high probability of being
assigned factor 1.

If assigning factors is to tricky I would settle for placing theValues
in a 10 x 15 matrix where the lower values would be more likely to end
up in column 1 (again, values close to -10 should have a really high
probability of being assigned to column 1.).

Any ideas? I have thought at times I was painfully close only to
realize I was completely wrong.

Thanks,

That Economics Guy



More information about the R-help mailing list