Data Types in R

For more information, please refer to Quick-R.

R has a wide variety of data types including scalars, vectors (numerical, character, logical), matrices, data frames, and lists.

Data Frames

In a data frame, different columns can have different modes (numeric, character, factor, etc.). This is similar to SAS and SPSS datasets.

##      ID Value Passed
## 1   one   1.0   TRUE
## 2   two   2.0   TRUE
## 3 three   5.3   TRUE
## 4  four   6.0  FALSE
## 5  five  -2.0   TRUE

Operators in R

Arithmetic Operators include:

Operator Description
+ addition
- subtraction
* multiplication
/ division
^ or ** exponentiation

Logical Operators include:

Operator Description
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to

R for Loop

Loops are used in programming to repeat a specific block of code.

Syntax of for loop

Example: for loop

Below is an example to count the number of even numbers in a vector.

## [1] 3

The Central Limit Theorem (CLT)

  • The CLT states that the sums of a set of random variables \((X_1, X_2, X_3, ..., X_n)\) is normally distributed no matter the distribution the individual X’s were sampled from, as long as they were sampled from identical distributions.

A simulation experiment

\[\begin{align*} Y_{i} = \sum\limits_{j=1}^{j=m} X_{ij} \alpha_{j} + \epsilon_i \end{align*}\]

  • For a given individual ( \(i=1\) ) with a number of loci ( \(m=1,000\) )
  • Each allele is \(X_j \in (A, a)\) , with the probability of \(p\) or \(1-p\)
  • The effect of \(j\)th allele ( \(\alpha_j\) ) can be samples from any distribution (e.g., uniform distribution)

According to the CLT, if \(m\) is sufficiently large, the sum is normally distributed.

Simulate one dividual

Simulate an individual’s phenotypic value. In this individual, the phenotype is determined by m number of markers with marker freq = 0.5. The markers’ effects ( \(\alpha\) ) are randomly draws from a uniform distribution.

## [1] 250.1851

Apply the above procedure to a population composed of n individuals.