Basic R Operations

Author

Jiaye Xu

Published

February 15, 2022

BASIC R OPERATIONS: Simple operations and lists

1. Define a variable and look at it

x <- 12
x
[1] 12

2. Mathematical operations on a variable

x+2
[1] 14
x-2
[1] 10
x/2
[1] 6
x*2
[1] 24
x^2
[1] 144

3. Saving the result of mathematical operations in another variable

y <- x+2
y
[1] 14

3b. Showing the result of an assignment as you do it (sometimes handy for debugging)

(y <- x+2)
[1] 14

4. Define a (atomic) vector with the c() function

x <- c(1,2,3,4)
x
[1] 1 2 3 4

5. Mathematical operations on a vector

### element-wise operation
x+2
[1] 3 4 5 6
x-2
[1] -1  0  1  2
x/2
[1] 0.5 1.0 1.5 2.0
x*2
[1] 2 4 6 8
x^2
[1]  1  4  9 16

5b. Example of R not handling matrix operations correctly

y <- c(1,2,3,4,5,6,7,8)
y
[1] 1 2 3 4 5 6 7 8
x*y
[1]  1  4  9 16  5 12 21 32

5c. Example of matrix multiplication

x %*% t(x) # (4 by 1) times (1 by 4)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    2    4    6    8
[3,]    3    6    9   12
[4,]    4    8   12   16
# equivalently, crossprod(t(x))

t(x) %*% x # (1 by 4) times (4 by 1)
     [,1]
[1,]   30
# equivalently, crossprod(x)

x%*%t(y) # (4 by 1) times (1 by 8)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    4    5    6    7    8
[2,]    2    4    6    8   10   12   14   16
[3,]    3    6    9   12   15   18   21   24
[4,]    4    8   12   16   20   24   28   32
# equivalently, crossprod(t(x), t(y))

#   Note: the function t(x) takes the transpose of x

5d. Matrix function

z1 <- matrix(y, 2) # of row = 2, the matrix is filled by columns by default.
z1
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
z2 <- matrix(y, 2, byrow=TRUE)
z2
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

6. Structure manipulation of vectors

# Select a specific element
x[1]
[1] 1
x[2]
[1] 2
# Select a range of elements
x[2:4]
[1] 2 3 4
# Drop a specific element
x[-2]
[1] 1 3 4
# Drop a range of elements
x[-(2:3)]
[1] 1 4
# Use list to select and drop elements
y<-c(1,3)
x[y]
[1] 1 3
x[-y]
[1] 2 4
# Combine two vectors
x<-c(x,y)
x
[1] 1 2 3 4 1 3
# Reverse a vector
rev(x)
[1] 3 1 4 3 2 1

7. Logical operations

# Test equality
1==5
[1] FALSE
1==1
[1] TRUE
# Test inequality
1!=5
[1] TRUE
1!=1
[1] FALSE

8. Ranges

1:3
[1] 1 2 3
seq(from=0, to=1, by=0.12)
[1] 0.00 0.12 0.24 0.36 0.48 0.60 0.72 0.84 0.96
seq(from=0, to=1, length=7)
[1] 0.0000000 0.1666667 0.3333333 0.5000000 0.6666667 0.8333333 1.0000000

USEFUL FUNCTIONS

1. Mean, variance, standard deviation

x <- c(1,2,3,4)
mean(x)
[1] 2.5
var(x)  
[1] 1.666667
sd(x)
[1] 1.290994

2. Summing a vector

sum(x)
[1] 10
prod(x)
[1] 24

3. Length of a vector

length(x)
[1] 4

4. Densities for various distributions

dnorm(0, mean=0, sd=1)      # Normal distribution
[1] 0.3989423
dexp(1, rate=1)         # Exponential distribution
[1] 0.3678794
dbinom(5, size=10, prob=0.5)    # Binomial distribution
[1] 0.2460938
dpois(10, lambda=10)        # Poisson distribution
[1] 0.12511
dgamma(1, shape=1, scale=1) # Gamma distribution
[1] 0.3678794
# NOTE: Here the density function is being evaluated at the given x with the specified parameters

5. CDFs, inverse CDFs, and random generation

pnorm(0, mean=0, sd=1)      # P(X<=0)
[1] 0.5
qnorm(0.95, mean=0, sd=1)   # quantile related to 95% lower tail probability
[1] 1.644854
rnorm(10, mean=0, sd=1)     # generates 10 realizations of a standard normal RV
 [1]  0.6572070  0.4243125  0.6040215  0.7544640 -0.5982136 -1.3667800
 [7]  0.6263866 -1.0903408 -1.4621166 -0.1400075

6. Basic plotting

x<-c(1,2,3,4)
y<-x^2
plot(x,y)

plot(x,y, xlab="x label here", ylab="y label here", pch=2,col="blue")

# NOTE: Please make sure to always use labels! Otherwise your plots are not helpful for other people (e.g. graders) interpreting your plots.

PROGRAMMING AND FLOW CONTROL

1. FOR loops

for(x in 1:10){
    print(x+1)
}
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11

2. WHILE loops

x<-1
while(x<10){
    print(x)
    x<-x+x
}
[1] 1
[1] 2
[1] 4
[1] 8
x<-1
while(x<10){
  x<-x+x
  print(x)
}
[1] 2
[1] 4
[1] 8
[1] 16

3. Defining functions

# ex. factorial 
Fact <- function(n) if (n == 1) 1 else n * Fact(n - 1)
Fact(5)
[1] 120
# NOTE: equivalently, use r function factorial(5), i.e., 5!