Converting factors to bounded random numerical data
You need to include how many random numbers you want to create; you
are asking for only 1, that is why they are are the same. You
probably want something like:
a$Z<-ifelse(a$Z=="L"
,sample(1:4, nrow(a), TRUE)
,ifelse(a$Z=="M"
,sample(5:9, nrow(a), TRUE)
,ifelse(a$Z=="U"
,sample(10:12, nrow(a), TRUE)
, -42 # you had a character which would have converted
everything to character
)
)
)
On Wed, Oct 10, 2012 at 8:36 PM, KoopaTrooper <ncooper1 at tulane.edu> wrote:
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.
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.