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 -- Thanoon Y. Thanoon PhD Candidate Department of Mathematical Sciences Faculty of Science University Technology Malaysia, UTM E.Mail: Thanoon.younis80 at gmail.com E.Mail: dawn_prayer80 at yahoo.com Facebook:Thanoon Younis AL-Shakerchy Twitter: Thanoon Alshakerchy H.P:00601127550205 [[alternative HTML version deleted]]
If I understand correctly ?sample On 16/09/2015 18:11, thanoon younis 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 > > > > >-- Michael http://www.dewey.myzen.co.uk/home.html
Hello, Try ?sample. Hope this helps, Rui Barradas Em 16-09-2015 18:11, thanoon younis escreveu:> 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 > > > > >
There is a version of sample especially for integers:> Q1 <- matrix(sample.int(4, 200, replace=TRUE), 200) > str(Q1)int [1:200, 1] 1 4 4 2 3 3 4 4 2 3 ... ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of thanoon younis Sent: Wednesday, September 16, 2015 12:11 PM To: r-help at r-project.org Subject: [R] generate ordered categorical variable in R 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 -- Thanoon Y. Thanoon PhD Candidate Department of Mathematical Sciences Faculty of Science University Technology Malaysia, UTM E.Mail: Thanoon.younis80 at gmail.com E.Mail: dawn_prayer80 at yahoo.com Facebook:Thanoon Younis AL-Shakerchy Twitter: Thanoon Alshakerchy H.P:00601127550205 [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
> On Sep 16, 2015, at 12:11 PM, 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 advanceYou are sampling from a uniform distribution on the continuous interval from 1 to 4. See ?sample set.seed(1)> sample(4, 200, replace = TRUE)[1] 2 2 3 4 1 4 4 3 3 1 1 1 3 2 4 2 3 4 2 4 4 1 3 1 2 2 1 2 4 2 2 3 2 [34] 1 4 3 4 1 3 2 4 3 4 3 3 4 1 2 3 3 2 4 2 1 1 1 2 3 3 2 4 2 2 2 3 2 [67] 2 4 1 4 2 4 2 2 2 4 4 2 4 4 2 3 2 2 4 1 3 1 1 1 1 1 3 4 4 4 2 2 4 [100] 3 3 2 2 4 3 1 1 2 4 3 4 3 2 2 1 1 3 1 2 3 4 2 2 1 4 2 3 1 1 3 3 1 [133] 1 3 4 3 3 3 4 3 3 3 1 2 3 2 1 3 1 4 3 3 2 2 3 1 3 1 2 1 2 4 2 4 4 [166] 2 1 2 3 2 3 4 4 2 2 4 3 3 3 4 2 1 4 3 4 1 4 3 4 3 3 2 1 4 2 3 1 4 [199] 2 4 Here, the result is a vector of integers. set.seed(1)> sample(factor(1:4), 200, replace = TRUE)[1] 2 2 3 4 1 4 4 3 3 1 1 1 3 2 4 2 3 4 2 4 4 1 3 1 2 2 1 2 4 2 2 3 2 [34] 1 4 3 4 1 3 2 4 3 4 3 3 4 1 2 3 3 2 4 2 1 1 1 2 3 3 2 4 2 2 2 3 2 [67] 2 4 1 4 2 4 2 2 2 4 4 2 4 4 2 3 2 2 4 1 3 1 1 1 1 1 3 4 4 4 2 2 4 [100] 3 3 2 2 4 3 1 1 2 4 3 4 3 2 2 1 1 3 1 2 3 4 2 2 1 4 2 3 1 1 3 3 1 [133] 1 3 4 3 3 3 4 3 3 3 1 2 3 2 1 3 1 4 3 3 2 2 3 1 3 1 2 1 2 4 2 4 4 [166] 2 1 2 3 2 3 4 4 2 2 4 3 3 3 4 2 1 4 3 4 1 4 3 4 3 3 2 1 4 2 3 1 4 [199] 2 4 Levels: 1 2 3 4 Here, the result is a factor. set.seed(1)> sample(factor(c("A", "B", "C", "D")), 200, replace = TRUE)[1] B B C D A D D C C A A A C B D B C D B D D A C A B B A B D B B C B [34] A D C D A C B D C D C C D A B C C B D B A A A B C C B D B B B C B [67] B D A D B D B B B D D B D D B C B B D A C A A A A A C D D D B B D [100] C C B B D C A A B D C D C B B A A C A B C D B B A D B C A A C C A [133] A C D C C C D C C C A B C B A C A D C C B B C A C A B A B D B D D [166] B A B C B C D D B B D C C C D B A D C D A D C D C C B A D B C A D [199] B D Levels: A B C D Here, the result is a factor. Just depends upon what you want for the categorial variable. Regards, Marc Schwartz
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) 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 > > > > > > -- > Thanoon Y. Thanoon > PhD Candidate > Department of Mathematical Sciences > Faculty of Science > University Technology Malaysia, UTM > E.Mail: Thanoon.younis80 at gmail.com > E.Mail: dawn_prayer80 at yahoo.com > Facebook:Thanoon Younis AL-Shakerchy > Twitter: Thanoon Alshakerchy > H.P:00601127550205 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
> 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():> samplefunction (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