Click here for videos

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]  0.0962578  1.3366320 -0.7827090  0.3168257  0.9596083 -0.6860354
##  [7]  0.1484898  0.3031658  0.6301796 -0.8647272
Y = X ^ 2      # square each number
Y
##  [1] 0.009265564 1.786585186 0.612633329 0.100378515 0.920848044
##  [6] 0.470644511 0.022049233 0.091909523 0.397126298 0.747753123
Y = X * 2      # multiple 2 to each number
Y
##  [1]  0.1925156  2.6732641 -1.5654179  0.6336514  1.9192166 -1.3720707
##  [7]  0.2969797  0.6063317  1.2603592 -1.7294544
Y = exp(X)     # take exponential power of each number
Y
##  [1] 1.1010429 3.8062027 0.4571659 1.3727633 2.6106736 0.5035686
##  [7] 1.1600810 1.3541390 1.8779478 0.4211664



3. Descriptive statistics

X = rnorm(10)  # draw random sample of n=10 from Standard Normal
X
##  [1]  0.6274894 -1.1606364 -0.8862771 -0.4122007 -1.5024925 -1.6102702
##  [7] -1.7335872  0.8976902 -1.9628536  0.6891917
length(X)      # get length of X
## [1] 10
mean(X)        # sample mean
## [1] -0.7053946
var(X)         # variance
## [1] 1.189978
sd(X)          # Standard Deviation
## [1] 1.090861
summary(X)     # 5-number summary
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -1.9629 -1.5833 -1.0235 -0.7054  0.3676  0.8977


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.2520712



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