> For the complete documentation index, see [llms.txt](https://r-pedia.gitbook.io/cevi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r-pedia.gitbook.io/cevi/data-types/dealing-with-number.md).

# -Dealing with Number

## #How to manage the numeric type (integer vs. double)

#### Numeric Types (integer vs. double):&#x20;

&#x20;R automatically converts  integers and double for  mathematical purposes

#### Creating Integer and Double Vectors: <a href="#integer" id="integer"></a>

&#x20;By default, `c()` function will produce a vector of double  numeric values. Create integer by placing an L after each number

```r
# create a double datatyp
double_var <- c(5, 9.5, 89.5)  

# placing an L after the values creates integers
integer_var <- c(1L, 6L, 10L)
```

#### Numeric Type Test: <a href="#type" id="type"></a>

```r
typeof(double_var)
## [1] "double"
typeof(integer_var)
## [1] "integer"
```

#### Converting Between Integer and Double Values: <a href="#convert" id="convert"></a>

&#x20;By default,  using the `x <- 1:10` method is integer data type. Change the datatyp with this methode:

```r
as.double(integer_var)

# identical to as.double()
as.numeric(int_var)

as.integer(double_var)
```

## #The different ways of generating non-random numbers

#### Specifing Numbers within a Sequence: <a href="#seq1" id="seq1"></a>

```r
1:10         
##  [1]  1  2  3  4  5  6  7  8  9 10
c(1, 5, 10)   
## [1]  1  5 10
# save the vector of integers between 1 and 10 as object x
x <- 1:10 
x
##  [1]  1  2  3  4  5  6  7  8  9 10
```

#### Generating Regular Sequences: <a href="#seq2" id="seq2"></a>

```r
seq(from = 1, to = 21, by = 2)             
##  [1]  1  3  5  7  9 11 13 15 17 19 21
seq(0, 21, length.out = 15)    
###########
##  [1]  0.0  1.5  3.0  4.5  6.0  7.5  9.0 10.5 
## 12.0 13.5 15.0 16.5 18.0 19.5
## [15] 21.0
```

#### Generating Repeated Sequences: <a href="#seq3" id="seq3"></a>

```r
rep(1:4, times = 2)   
## [1] 1 2 3 4 1 2 3 4
rep(1:4, each = 2)    
## [1] 1 1 2 2 3 3 4 4
```

## #The different ways of generating random numbers

&#x20; R has  pseudo-random number generators that allow you to simulate the most common probability distributions.

#### Uniform numbers: <a href="#uniform" id="uniform"></a>

```r
# generate n random numbers between the default values of 0 and 1
runif(n)            

# generate n random numbers between 0 and 25
runif(n, min = 0, max = 25)       

# generate n random numbers between 0 and 25 (with replacement)
sample(0:25, n, replace = TRUE)   

# generate n random numbers between 0 and 25 (without replacement)
sample(0:25, n, replace = FALSE)
```

Non-uniform probability distribution have four primary functions:

* `r`: random number generation
* `d`: density or probability mass function
* `p`: cumulative distribution
* `q`: quantiles

#### Normal Distribution Numbers:  <a href="#normal" id="normal"></a>

```r
# generate n random numbers from a normal distribution with given 
# mean & st. dev.
rnorm(n, mean = 0, sd = 1)    

# generate CDF probabilities for value(s) in vector q 
pnorm(q, mean = 0, sd = 1)    

# generate quantile for probabilities in vector p
qnorm(p, mean = 0, sd = 1)    

# generate density function probabilites for value(s) in vector x
dnorm(x, mean = 0, sd = 1)
```

#### Binomial Distribution Numbers: <a href="#binomial" id="binomial"></a>

```r
rbinom(n, size = 100, prob = 0.5)  

# generate CDF probabilities for value(s) in vector q
pbinom(q, size = 100, prob = 0.5) 

# generate quantile for probabilities in vector p
qbinom(p, size = 100, prob = 0.5) 

# generate density function probabilites for value(s) in vector x
dbinom(x, size = 100, prob = 0.5) 
```

#### Poisson Distribution Numbers: <a href="#poisson" id="poisson"></a>

```r
rpois(n, lambda = 4)  

# generate CDF probabilities for value(s) in vector q when 
# lambda (mean rate) 
# equals 4.
ppois(q, lambda = 4)  

# generate quantile for probabilities in vector p when 
# lambda (mean rate) 
# equals 4.
qpois(p, lambda = 4)  

# generate density function probabilites for value(s) in vector x when 
#lambda 
# (mean rate) equals 4.
dpois(x, lambda = 4) 
```

#### Exponential Distribution Numbers: <a href="#exponential" id="exponential"></a>

```r
rexp(n, rate = 1)   
# generate CDF probabilities for value(s) in vector q when rate = 4.
pexp(q, rate = 1)   

# generate quantile for probabilities in vector p when rate = 4.
qexp(p, rate = 1)   

# generate density function probabilites for value(s) in vector x 
# when rate = 4.
dexp(x, rate = 1)  
```

#### Gamma Distribution Numbers: <a href="#gamma" id="gamma"></a>

```r
rgamma(n, shape = 1)   

# generate CDF probabilities for value(s) in vector q when 
# shape parameter = 1.
pgamma(q, shape = 1)   

# generate quantile for probabilities in vector p when 
# shape parameter = 1.
qgamma(p, shape = 1)   

# generate density function probabilites for value(s) in vector x 
# when shape 
# parameter = 1.
dgamma(x, shape = 1)
```

## #Setting Seed Values

```r
set.seed(197)
rnorm(n = 2, mean = 0, sd = 1)
##  [1]  0.6091700 -1.4391423  

set.seed(197)
rnorm(n = 10, mean = 0, sd = 1)
##  [1]  0.6091700 -1.4391423  
```

## #Comparing Numeric Values

#### Comparison Operators: <a href="#numeric_comparison" id="numeric_comparison"></a>

```r
x < y     # is x less than y
x > y     # is x greater than y
x <= y    # is x less than or equal to y
x >= y    # is x greater than or equal to y
x == y    # is x equal to y
x != y    # is x not equal to y

x <- 9
y <- 10
x == y

x <- c(1, 4, 9, 12)
y <- c(4, 4, 9, 13)

x == y

# How many pairwise equal values are in vectors x and y
sum(x == y)    

# Where are the pairwise equal values located in vectors x and y
which(x == y)
```

#### Exact Equality: <a href="#numeric_exact" id="numeric_exact"></a>

```r
x <- c(4, 4, 9, 12)
y <- c(4, 4, 9, 13)

identical(x, y)

x <- c(4, 4, 9, 12)
y <- c(4, 4, 9, 12)

identical(x, y)
```

#### Floating Point Comparison: <a href="#numeric_near" id="numeric_near"></a>

```r
x <- c(4.00000005, 4.00000008)
y <- c(4.00000002, 4.00000006)

all.equal(x, y)

# If the difference is greater than the tolerance level 
# the function will return the mean relative difference:
x <- c(4.005, 4.0008)
y <- c(4.002, 4.0006)

all.equal(x, y)
## [1] "Mean relative difference: 0.0003997102"
```

## #Rounding numeric Values

```r
x <- (1, 1.35, 1.7, 2.05, 2.4)
# Round to the nearest integer
round(x)
##  [1] 1 1 2 2 2 

# Round up
ceiling(x)
##  [1] 1 2 2 3 3
 
# Round down
floor(x)
##  [1] 1 1 1 2 2 
 
# Round to a specified decimal
round(x, digits = 1)
##  [1] 1.0 1.4 1.7 2.0 2.4
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r-pedia.gitbook.io/cevi/data-types/dealing-with-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
