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

Charles C. Berry cberry at tajo.ucsd.edu
Sun Mar 28 19:25:02 CEST 2010


On Sun, 28 Mar 2010, Economics Guy wrote:

>> It is really the same thing. One of many possibilities:
>>
>>> theFrame <- data.frame(theValues=runif(150,-10,10))
>>> exact <- diag(15)[1+ (rank(theFrame$theValues)-1)%/%10,]
>>> not.so.exact <- diag(15)[1+
>>> (rank(theFrame$theValues+runif(150,0,3))-1)%/%10,]
>>
>> If what you actually wanted was one factor with fifteen levels, just wrap
>> the subscript in the last assignment in factor() instead.
>>
>> HTH,
>>
>> Chuck
>
> Thanks Chuck, this does what I asked for:
>
> theValues <- runif(150,-10,10)
> exact <- factor(1+(rank(theValues)-1)%/%100)
>
> Unfortunately, it looks like my example may have been too contrived
> for my actual program.
>
> In the solution that Chuck proposed he antisipated that I may not want
> the assignment of factor levels to be exact:
>
> notSoExact <- factor(1+(rank(theValues+runif(150,0,3)-1)%/%100)
>
> This is close to what I need. However, in the real program what I need
> to be able to do is precisely vary the degree of exactness in the
> assignment. So I need to be able to have the assignment range from
> completely random assignment to the exact assignment that "exact"
> above provides.
>
> Anyone think of a simple way to do this?

That's div 10, not div 100 (check your typing).

The range of the runif() call determines the 'exactness'.

Alternatively, apply weights of a and  1-a, for 0<=a<=1, to the components 
in rank().

Chuck

>
> Thanks Agan,
>
> That Economics Guy
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list