R programming: Generating random frequencies

0 votes

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
• 9,900 points
58 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,320 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
898 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,130 points
875 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,290 points
129 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
• 32,440 points
143 views
0 votes
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
• 32,440 points
183 views
+9 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 8, 2018 in Data Analytics by Anmol
• 3,680 points
97 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 Anmol
• 3,680 points
107 views
0 votes
1 answer

How to know the type of an object?

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

answered Sep 19, 2018 in Data Analytics by Anmol
• 3,680 points
46 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 29, 2019 in Data Analytics by Tyrion anex
• 8,320 points
43 views
0 votes
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,320 points
40 views