1. R is case sensitive

X = 3   # This assigns 3 to object X. But you can't see it.
        X       # now you see what's inside X
## [1] 3
x       # "x" is not same as "X".



2. Vectors

R uses vectors.

X = c(1, 4, 6)      # create vector
        X
## [1] 1 4 6
X = 1:10            # 1 through 10
        X
##  [1]  1  2  3  4  5  6  7  8  9 10
X = seq(1,30, 4)    # 1 to 30 in step of 2
        X
## [1]  1  5  9 13 17 21 25 29
X = rep(0, 10)
        X
##  [1] 0 0 0 0 0 0 0 0 0 0


Apply funciton to vector

When you apply a function to a vector, it usually work on EACH element in the vector.

X = rnorm(10)  # draw random sample of size 10 from Standard Normal
            X
##  [1]  1.4530453 -1.2072829 -0.3514677
##  [4]  0.2453460  1.0365689  0.3723902
##  [7]  0.5971987 -0.2934279 -0.1469856
## [10]  0.4789901
Y = X ^ 2      # square each number
            Y
##  [1] 2.11134069 1.45753199 0.12352954
##  [4] 0.06019465 1.07447499 0.13867447
##  [7] 0.35664627 0.08609995 0.02160475
## [10] 0.22943154
Y = X * 2      # multiple 2 to each number
            Y
##  [1]  2.9060906 -2.4145658 -0.7029354
##  [4]  0.4906920  2.0731377  0.7447804
##  [7]  1.1943974 -0.5868559 -0.2939711
## [10]  0.9579803
Y = exp(X)     # take exponential power of each number
            Y
##  [1] 4.2761168 0.2990086 0.7036546 1.2780634
##  [5] 2.8195262 1.4511992 1.8170216 0.7457030
##  [9] 0.8633064 1.6144432



3. Descriptive statistics

X = rnorm(10)  # draw random sample of n=10 from Standard Normal
        X
##  [1]  0.23517920  0.87486899  1.09611778
##  [4]  0.98434840 -0.27236446  0.70784119
##  [7]  0.01653147  0.82634676  2.59635950
## [10]  1.61081816
length(X)      # get length of X
## [1] 10
mean(X)        # sample mean
## [1] 0.8676047
var(X)         # variance
## [1] 0.6731004
sd(X)          # Standard Deviation
## [1] 0.8204269
summary(X)     # 5-number summary
##    Min. 1st Qu.  Median    Mean 3rd Qu.
## -0.2724  0.3533  0.8506  0.8676  1.0682
##    Max.
##  2.5964


Correlation

X = rnorm(10)  # draw random sample of n=10 from Standard Normal
Y = runif(10)  # draw random sample of n=10 from Unif(0,1)
cor(X,Y)       # correlation between vector X and vector Y
## [1] -0.2921617



Code Only

### 1. R is case sensitive
X = 3   # This assigns 3 to object X. But you can't see it.
X       # now you see what's inside X

x       # "x" is not same as "X".


### 2. Vectors
X = c(1, 4, 6)      # create vector
X
X = 1:10            # 1 through 10
X
X = seq(1,30, 4)    # 1 to 30 in step of 2
X
X = rep(0, 10)
X


# Apply funciton to vector
X = rnorm(10)  # draw random sample of size 10 from Standard Normal
X
Y = X ^ 2      # square each number
Y
Y = X * 2      # multiple 2 to each number
Y
Y = exp(X)     # take exponential power of each number
Y


### 3. Descriptive statistics
X = rnorm(10)  # draw random sample of n=10 from Standard Normal
X
length(X)      # get length of X
mean(X)        # sample mean
var(X)         # variance
sd(X)          # Standard Deviation
summary(X)     # 5-number summary

# Correlation
X = rnorm(10)  # draw random sample of n=10 from Standard Normal
Y = runif(10)  # draw random sample of n=10 from Unif(0,1)
cor(X,Y)       # correlation between vector X and vector Y