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 in Data Analytics by Sophie may
• 9,530 points
47 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 by Tyrion anex
• 8,270 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,700 points
416 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
532 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 darklord
• 6,140 points
94 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 in Data Analytics by anonymous
• 25,760 points
85 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 in Data Analytics by anonymous
• 25,760 points
119 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,620 points
54 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,620 points
33 views
0 votes
1 answer

Remove NA values from the output in R programming

Edit your code: columnmean <- function(x, removeNA = ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 40,440 points
40 views
0 votes
1 answer
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,270 points
26 views