Hello everyone, I have two questions: 1.) I would like to generate random percentages that add up to 100. For example, if I need 5 percentages, I would obtain something like: 20, 30, 40, 5, 5. Is there some way to do this in R? 2.) I would like to insert vectors of specified length into a larger vector of specified length randomly, and fill the gaps with zeroes. For example, if I have 3 vectors of length 3, 2, and 2 with values and I would like to randomly place them into a vector of length 25 made of 0's. Thank you in advance! -Aaron [[alternative HTML version deleted]]
On Oct 27, 2010, at 11:02 AM, Aaron Lee wrote:> Hello everyone, > > I have two questions: > > 1.) I would like to generate random percentages that add up to 100. > For > example, if I need 5 percentages, I would obtain something like: 20, > 30, 40, > 5, 5. Is there some way to do this in R? > > 2.) I would like to insert vectors of specified length into a larger > vector > of specified length randomly, and fill the gaps with zeroes. For > example, if > I have 3 vectors of length 3, 2, and 2 with values and I would like to > randomly place them into a vector of length 25 made of 0's.You need to specify the order of placement and whether over-writing is allowed. If overwriting is not allowed then after the first is placed the allowable positions may shrink in odd ways.> > Thank you in advance! >David Winsemius, MD West Hartford, CT
Jonathan P Daily
2010-Oct-27 15:25 UTC
[R] Generate random percentages and placing vectors
1)
rands <- runif(5)
rands <- rands/sum(rands)*100
2)
# assume vectors are v1, v2, etc.
v_all <- c(v1, v2, ...)
v_len <- length(v_all)
output <- rep(0,25)
output[sample(1:25, v_len)] <- v_all
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
the thing itself have purpose? Or do we, what's the word... imbue it."
- Jubal Early, Firefly
From:
Aaron Lee <aaron.zl.lee@gmail.com>
To:
r-help@r-project.org
Date:
10/27/2010 11:06 AM
Subject:
[R] Generate random percentages and placing vectors
Sent by:
r-help-bounces@r-project.org
Hello everyone,
I have two questions:
1.) I would like to generate random percentages that add up to 100. For
example, if I need 5 percentages, I would obtain something like: 20, 30,
40,
5, 5. Is there some way to do this in R?
2.) I would like to insert vectors of specified length into a larger
vector
of specified length randomly, and fill the gaps with zeroes. For example,
if
I have 3 vectors of length 3, 2, and 2 with values and I would like to
randomly place them into a vector of length 25 made of 0's.
Thank you in advance!
-Aaron
[[alternative HTML version deleted]]
______________________________________________
R-help@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.
[[alternative HTML version deleted]]