KoopaTrooper
2012-Oct-11 00:08 UTC
[R] Converting factors to bounded random numerical data
I have a data set (a) with 3 columns (X,Y,Z). The first 2 columns are numerical. The third column (Z) is a factor with three levels ("A","B","C"). What I want to do is turn each of the "A's" into different random numbers between 1 and 4, "B's" into a random number between 5 and 8, etc. I tried this: a$Z<-ifelse(a$Z=="A",sample(1:4,1),ifelse(a$Z=="B",sample(5:9,1),ifelse(a$Z=="C",sample(10:12,1),"") That almost worked but changed all the A's to the same random number. I want a different random number for each "A". Ideas? -- View this message in context: http://r.789695.n4.nabble.com/Converting-factors-to-bounded-random-numerical-data-tp4645798.html Sent from the R help mailing list archive at Nabble.com.
KoopaTrooper
2012-Oct-11 00:36 UTC
[R] Converting factors to bounded random numerical data
I have a data set (a) with three columns (X,Y,Z). The first 2 columns are numeric. The third (Z) is a factor with three levels A,B,C. I want to turn each A into a different random number between 1 and 4, each B into a different random number between 5 and 8, etc. I tried this: a$Z<-ifelse(a$Z=="L",sample(1:4,1),ifelse(a$Z=="M",sample(5:9,1),ifelse(a$Z=="U",sample(10:12,1),"") and it almost worked but changed all the "A's" into the same random number. I need a different random number for each A. Ideas? Thanks, -- View this message in context: http://r.789695.n4.nabble.com/Converting-factors-to-bounded-random-numerical-data-tp4645801.html Sent from the R help mailing list archive at Nabble.com.
William Dunlap
2012-Oct-11 01:04 UTC
[R] Converting factors to bounded random numerical data
sample(Range,1) generates 1 random number. Change that (in all 3 places) to sample(Range, length(a$Z), replace=TRUE) to get length(a$Z) random numbers in the same range. There are other ways to do this with less typing. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of KoopaTrooper > Sent: Wednesday, October 10, 2012 5:09 PM > To: r-help at r-project.org > Subject: [R] Converting factors to bounded random numerical data > > I have a data set (a) with 3 columns (X,Y,Z). The first 2 columns are > numerical. The third column (Z) is a factor with three levels ("A","B","C"). > What I want to do is turn each of the "A's" into different random numbers > between 1 and 4, "B's" into a random number between 5 and 8, etc. > > I tried this: > > a$Z<- > ifelse(a$Z=="A",sample(1:4,1),ifelse(a$Z=="B",sample(5:9,1),ifelse(a$Z=="C",sample(10:1 > 2,1),"") > > That almost worked but changed all the A's to the same random number. I want > a different random number for each "A". Ideas? > > > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Converting-factors-to- > bounded-random-numerical-data-tp4645798.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.