R programming Generating random frequencies

+1 vote

I'm trying to generate frequencies randomly to understand gene frequencies in a population using R. I tried this:

freq<- function(x,y)  #random frequency generator
    {rn<-sample(1:y,x)
    sum <- sum(rn)
    for(i in 1:x){
    RG[i]=rn[i]/sum}
    return(RG)
    }

Does anyone know how to constrain the sum to a particular value (say, 100) before division?

Feb 18, 2019 in Data Analytics by Sophie may
• 10,620 points
880 views

1 answer to this question.

0 votes

Follow this, it might help you:

  1. Produce sample from uniform distribution
  2. Sort the values
  3. Insert 0 and 1
  4. Use thresholds as cut-off points for values

Refer this example:

x <- sort(runif(10))
x
## [1] 0.06178627 0.20168193 0.26550866 0.37212390 0.57285336 0.62911404 0.66079779 0.89838968 0.90820779 0.94467527
x <- c(0,x,1)
y <- diff(x)
y
## [1] 0.061786270 0.139895661 0.063826732 0.106615236 0.200729464 0.056260681 0.031683749 0.237591892 0.009818105 0.036467479 0.055324731
sum(y)
## [1] 1
answered Feb 18, 2019 by Tyrion anex
• 8,700 points

Related Questions In Data Analytics

0 votes
1 answer

Sample random rows in DataFrame in R

First we should have some data: df1<-data.frame(matrix(rnorm(20), nrow=10)) > ...READ MORE

answered Apr 13, 2018 in Data Analytics by DeepCoder786
• 1,720 points
8,137 views
0 votes
1 answer

What is a Random Walk model and how can you simulate it using R?

A random walk is a simple example ...READ MORE

answered Jul 3, 2018 in Data Analytics by DataKing99
• 8,250 points
3,230 views
0 votes
1 answer

Random Walk model in R

A random walk is a simple example ...READ MORE

answered Jul 10, 2018 in Data Analytics by Sahiti
• 6,370 points
972 views
0 votes
2 answers

What are the rules to define a variable name in R programming language?

The same rules almost follow for all ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
15,123 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,813 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,627 views
0 votes
1 answer

How can I perform word stemming in R

The tm package in R provides the stemDocument() function to stem the ...READ MORE

answered Aug 20, 2018 in Data Analytics by Abhi
• 3,720 points
4,537 views
0 votes
1 answer

How to know the type of an object?

To find the type of an object ...READ MORE

answered Sep 20, 2018 in Data Analytics by Abhi
• 3,720 points
712 views
0 votes
1 answer

R programming: Using Caret package to implement Random Forest

You can check out the official docs ...READ MORE

answered Apr 30, 2019 in Data Analytics by Tyrion anex
• 8,700 points
708 views
+1 vote
1 answer

R programming error

Alright, you can either use gsub to match the ...READ MORE

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,700 points
658 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP