[R] Assigning factors probabilistically based on the value of another variable.
Economics Guy
economics.guy at gmail.com
Sun Mar 28 14:37:47 CEST 2010
> 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?
Thanks Agan,
That Economics Guy
More information about the R-help
mailing list