# Biomedical Engineering Theory And Practice/R Language
## Data Types

R has a various objects for holding data, including scalars, vectors, matrices, arrays, data frames, and lists.

### Scalar and Constant

"Scalar" generally means "one-dimensional"vector. Constants only have one value ever. You can constants is similar to zero-dimensional values (a single point).
* Scalar
* Constant

### Vector

Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data. The combine function c is used to form the vector. Here are examples of each type of vector:

### Matrix

A matrix is a two-dimensional array where each element has the same mode (numeric, character, or logical). Matrices are created with the "matrix" function . The general format is as follows:

### Lists
## Basic Functions
### Arithmetic Operators

The arithmetic operators and their examples which are used in R programming are listed in the table below.
| Function | R Command | Example |
| Exponentiation, $a^{n}$ | > a^b | > 3+6 [1] 9 |
| Multiplication, $a\times b$ | > a*b | > 22*5 [1] 110 |
| Division, $a\div b$ | > a/b | > 30/3 [1] 10 |
| Addition, $a+b$ | > a+b | > 10+9 [1] 19 |
| Subtraction, $a-b$ | > a-b | > 10-3 [1] 7 |
| Integer(Quotient) | > a%/%b | > 20%/%3 [1] 6 |
| Modulo(Remainder) | > a%%b | > 20%%3 [1] 2 |

### Complex Number

| | R command | | R command |
| Complex number | > Re(x) [1] 5.2 | Real part | > Im(x) [1] -3 |
| Imaginary part | > Im(x) [1] -3 | Modulus | > Mod(x) [1] 6.003332 |
| Argument | > Arg(x) [1] -0.5232783 | Conjugate | > Conj(x) [1] 5.2+3i |
| Membership | > is.complex(x) [1] TRUE | Coercion | > as.complex(19.6) [1] 19.6+0i |

### Rounding

| Function | R Command | Function | R Command |
| Greatest integer less than | > floor(9.9) [1] 9 > floor(-9.9) [1] -10 | Next integer | > ceiling(9.9) [1] 10 > ceiling(-9.9) [1] -9 |
| Rounding function | > round(9.9) [1] 10 > round(9.2) [1] 9 | Strip off the decimal | > trunc(8.6) [1] 8 > trunc(-8.6) [1] -8 |

### Trigonometric Functions

| Function | Trigometric Function | Trigometric Inverse Function | Hyperbolic Function | Hyperbolic Inverse Function |
| sine | sin(x) | asin(x) | sinh(x) | asinh(x) |
| cosine | cos(x) | acos(x) | cosh(x) | acosh(x) |
| tangent | tan(x) | atan(x) | tanh(x) | atanh(x) |

### Log and Exponential Functions

| Function | R command | R Example |
| Absolute,$|x|$ | abs(x) | > abs(-7.4) [1] 7.4 |
| Log to the base e,$\log _{e}(x)$ | > log(10) [1] 2.302585 | |
| Log to the base 10,$\log _{10}(x)$ | log10(x) | > log10(100) [1] 2 |
| Log to the base n of x | log(x,n) | > log(64,4) [1] 3 |
| $e^{x}$ | exp(x) | > exp(3) [1] 20.08554 |
| ${\sqrt {x}}$ | sqrt(x) | > sqrt(25) [1] 5 |
| $n!$ | factorial(x) | > factorial(10) [1] 3628800 |
| ${\frac {n!}{r!(n-r)!}}$ | combinations(n,r) | > choose(5,4) [1] 5 |

### Relational Operators and Logical Variables
#### Relational Operators

| Relational Operator | |
| Equal | == |
| Not equal | != |
| Less than | < |
| Greater than | > |
| Less than or equal | <= |
| Greater than or equal | >= |
* TRUE=1,FALSE=0

#### Logical Operators

| $A$ | $B$ | $!A$ | $A$ &$B$ | $A|B$ | $xor(A,B)$ |
| False(0) | False(0) | True(1) | False(0) | False(0) | False(0) |
| False(0) | True(1) | True(1) | False(0) | True(1) | True(1) |
| True(1) | False(0) | False(0) | False(0) | True(1) | True(1) |
| True(1) | True(1) | False(0) | True(1) | True(1) | False(0) |
> z1<-xor(x,y) > z1 [1] FALSE FALSE FALSE

### Random Number Generation
## Vector Functions
### Length and Statistics

| Function | R command | Function | R command |
| Length | > length(x) [1] 10 | Mean | > mean(x) [1] 25.3 |
| Max | > max(x) [1] 90 | Min | > min(x) [1] 0 |
| Distribution | > quantile(x) 0% 25% 50% 75% 100% 0.00 6.75 11.50 28.25 90.00 | Sort | > sort(x) [1] 0 2 6 9 11 12 14 33 76 90 |
| Function | R command |
| Reference the 5th element of Vector from the vector | > x[5] [1] 12 |
| Delete the 3rd element of vector from the vector | > x1<-x[-3] > x1 [1] 6 9 14 12 2 33 76 0 90 |
| Delete the last element of vector from the vector | > x2<-x[-length(x)] > x2 [1] 6 9 11 14 12 2 33 76 0 |
| Delete 1st and the last element of vector from the vector | > x3<-x[c(-1,-length(x))] > x3 [1] 9 11 14 12 2 33 76 0 |
| Remove the smallest 2 and the largest 3 element from the vector | > trim <-function(x)sort(x)[-c(1,2,length(x)-2,length(x)-1,length(x))] > trim(x) [1] 6 9 11 12 14 |
| | R code | |
| Sum | > sum(x) [1] 253 | |
| Mean,Median | > mean(x) [1] 25.3 | > median(x) [1] 11.5 |
| Range | > range(x) [1] 0 90 | |
| Standard Deviation,variance | > sd(x) [1] 31.87841 | > var(x) [1] 1016.233 |
| Which is the largest and smallest number | > which(x==max(x)) [1] 10 | > which(x==min(x)) [1] 9 |
| sort and reverse sort | > sort(x) [1] 0 2 6 9 11 12 14 33 76 90 | > rev(sort(x)) [1] 90 76 33 14 12 11 9 6 2 0 |