+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 153 views

## 1 answer to this question.

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
##  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
##  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``` answered Feb 18, 2019 by
• 8,670 points

## Sample random rows in DataFrame in R

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

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

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

## Random Walk model in R

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

+1 vote

## How to sort a data frame by columns in R?

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

## How can I perform word stemming in R

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

## How to know the type of an object?

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