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.
Converting factors to bounded random numerical data
5 messages · KoopaTrooper, Jeff Newmiller, jim holtman +1 more
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:
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.
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.
This should work:
a <- data.frame(X=rnorm(25, 20, 4), Y=rnorm(25, 15, 3),
Z=sample(c(LETTERS[1:3]), 25, replace=TRUE))
head(a)
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
Zfact <- data.frame(factor=LETTERS[1:3], low=c(1, 5, 10), high=c(4, 9,
12))
Zfact
factor low high 1 A 1 4 2 B 5 9 3 C 10 12
set.seed(42) a$Zval <- runif(nrow(a), Zfact$low[as.numeric(a$Z)],
Zfact$high[as.numeric(a$Z)])
head(a, 10)
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
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of jim holtman
Sent: Wednesday, October 10, 2012 8:32 PM
To: KoopaTrooper
Cc: r-help at r-project.org
Subject: Re: [R] 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.
______________________________________________ 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.
I forgot that you were looking for whole numbers:
Zfact <- data.frame(factor=LETTERS[1:3], low=c(.5, 4.5, 9.5),
high=c(4.499, 9.499, 12.499))
Zfact
factor low high 1 A 0.5 4.499 2 B 4.5 9.499 3 C 9.5 12.499
set.seed(42) a$Zval <- round(runif(nrow(a), Zfact$low[as.numeric(a$Z)],
Zfact$high[as.numeric(a$Z)]), 0)
head(a, 10) head(a, 10)
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
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of David L Carlson Sent: Wednesday, October 10, 2012 10:30 PM To: 'jim holtman'; 'KoopaTrooper' Cc: r-help at r-project.org Subject: Re: [R] Converting factors to bounded random numerical data This should work:
a <- data.frame(X=rnorm(25, 20, 4), Y=rnorm(25, 15, 3),
Z=sample(c(LETTERS[1:3]), 25, replace=TRUE))
head(a)
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
Zfact <- data.frame(factor=LETTERS[1:3], low=c(1, 5, 10), high=c(4,
9, 12))
Zfact
factor low high 1 A 1 4 2 B 5 9 3 C 10 12
set.seed(42) a$Zval <- runif(nrow(a), Zfact$low[as.numeric(a$Z)],
Zfact$high[as.numeric(a$Z)])
head(a, 10)
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
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of jim holtman
Sent: Wednesday, October 10, 2012 8:32 PM
To: KoopaTrooper
Cc: r-help at r-project.org
Subject: Re: [R] 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.
______________________________________________ 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.
______________________________________________ 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.