How to calculate Continuous Uniform distribution probabilities in R

We provide effective and economically affordable online training courses for R and Python, click here for more details and course registration !

Continuous Uniform distribution is used to model a flat probability existence with a range (A,B). Its probability density can be depicted as

Continuous Uniform Distribution probability density function

The follow figure shows probability density curve for a Continuous Uniform distribution defined with range (1,3).

A Continuous Uniform distribution probability densify curve

In R programming, we can use following functions in computation of Continuous Uniform distribution.

dunif() – for probability density ,

punif() – for cumulative probability,

qunif() – for quantile value ,

dunif() – for random number generation .

Following code snippet shows how to use these functions in RStudio.

# generating a vector of numbers
x_vec <- seq(0, 10 , by = 0.2) 
x_vec
#output
[1]  0.0  0.2  0.4  0.6  0.8  1.0  1.2  1.4  1.6  1.8  2.0  2.2  2.4
[14]  2.6  2.8  3.0  3.2  3.4  3.6  3.8  4.0  4.2  4.4  4.6  4.8  5.0
[27]  5.2  5.4  5.6  5.8  6.0  6.2  6.4  6.6  6.8  7.0  7.2  7.4  7.6
[40]  7.8  8.0  8.2  8.4  8.6  8.8  9.0  9.2  9.4  9.6  9.8 10.0# calculating #calculate probability density for these numbers in a Continuous Uniform
#probability distribution, with range defined on (3,8)
dunif(x_vec, min = 3, max = 8)
#output
[1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.2
[17] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[33] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[49] 0.0 0.0 0.0
#plot probability density 
plot(dunif(x_vec, min = 3, max = 8))
Probability density output from RStudio
#calculate cumulative probabilities
punif (x_vec , min =3 , max = 8)
#plot cumulative probabilities
plot(punif (x_vec , min =3 , max = 8))
Cumulative probabilities plot in RStudio
#create a vector for probalities
q_vec <- seq(0, 1 , by = 0.1) 
q_vec
#output
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0plot(qunif(q, min = 3, max = 8))
#quantile of these probability values
#quantile means the associated cumulative probability of values less than
#this quantile value
qunif(q_vec, min = 3, max = 8)
#output
3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
#random number generation, 10 such random numbers from Continuous Uniform
#distribution
runif(10, min=3, max=8) 
#output
 [1] 4.892670 3.914989 7.944297 3.637653 4.492242 7.196020 4.513473
 [8] 7.857784 6.017247 6.964857

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago