[R] generate ordered categorical variable in R
Marc Schwartz
marc_schwartz at me.com
Wed Sep 16 22:28:34 CEST 2015
> On Sep 16, 2015, at 1:06 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>
> Yikes! The uniform distribution is a **continuous** distribution over
> an interval. You seem to want to sample over a discrete distribution.
> See ?sample for that, as in:
>
> sample(1:4,100,rep=TRUE)
>
> ## or for this special case and faster
>
> sample.int(4,size=100,rep=TRUE)
Bert,
I am not sure that it is really faster, since internally, sample() calls sample.int():
> sample
function (x, size, replace = FALSE, prob = NULL)
{
if (length(x) == 1L && is.numeric(x) && x >= 1) {
if (missing(size))
size <- x
sample.int(x, size, replace, prob)
}
else {
if (missing(size))
size <- length(x)
x[sample.int(length(x), size, replace, prob)]
}
}
set.seed(1)
> system.time(x1 <- sample(1e10, 1e8, replace = TRUE))
user system elapsed
2.755 0.170 2.925
set.seed(1)
> system.time(x2 <- sample.int(1e10, 1e8, replace = TRUE))
user system elapsed
2.767 0.183 2.951
> all(x1 == x2)
[1] TRUE
Regards,
Marc
>
> Cheers,
> Bert
>
> Bert Gunter
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> -- Clifford Stoll
>
>
> On Wed, Sep 16, 2015 at 10:11 AM, thanoon younis
> <thanoon.younis80 at gmail.com> wrote:
>> Dear R- users
>>
>> I want to generate ordered categorical variable vector with 200x1 dimension
>> and from 1 to 4 categories and i tried with this code
>>
>> Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
>> decimals like 1.244, 2.342,4,321 and so on ... My question how can i
>> generate a vector and also a matrix with orered categorical variables and
>> without decimals just 1,2,3 ,4 ,1,2,3,4, ....
>>
>> Many thanks in advance
More information about the R-help
mailing list