Skip to content

Converting factors to bounded random numerical data

5 messages · KoopaTrooper, Jeff Newmiller, jim holtman +1 more

#
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.
#
Please don't double post.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
KoopaTrooper <ncooper1 at tulane.edu> wrote:

            
#
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:

  
    
#
This should work:
Z=sample(c(LETTERS[1:3]), 25, replace=TRUE))
X         Y Z
1  17.77449 14.425221 B
2  19.95400 13.408439 A
3  13.40162 12.219984 A
4  15.89822 19.214026 B
5  18.55717 14.568691 B
6  19.86619 11.606099 B
12))
factor low high
1      A   1    4
2      B   5    9
3      C  10   12
Zfact$high[as.numeric(a$Z)])
X        Y Z      Zval
1  17.77449 14.42522 B  8.659224
2  19.95400 13.40844 A  3.811226
3  13.40162 12.21998 A  1.858419
4  15.89822 19.21403 B  8.321791
5  18.55717 14.56869 B  7.566982
6  19.86619 11.60610 B  7.076384
7  14.72275 18.38715 A  3.209765
8  26.09711 17.79988 C 10.269333
9  18.72558 15.64477 C 11.313985
10 17.33280 19.03034 B  7.820259

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
I forgot that you were looking for whole numbers:
high=c(4.499, 9.499, 12.499))
factor low   high
1      A 0.5  4.499
2      B 4.5  9.499
3      C 9.5 12.499
Zfact$high[as.numeric(a$Z)]), 0)
X        Y Z Zval
1  17.77449 14.42522 B    9
2  19.95400 13.40844 A    4
3  13.40162 12.21998 A    2
4  15.89822 19.21403 B    9
5  18.55717 14.56869 B    8
6  19.86619 11.60610 B    7
7  14.72275 18.38715 A    3
8  26.09711 17.79988 C   10
9  18.72558 15.64477 C   11

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352