1. Create vectors ‘u’ & ‘v’; compare ‘u’ > ‘v’
u <- c(5,2,8)
v <- c(1,3,9)
u > v
## [1]  TRUE FALSE FALSE
  1. Create function = x+1 & apply function to vectors ‘u’ & ‘v’
w <- function(x) return(x+1)
w(u)
## [1] 6 3 9
w(v)
## [1]  2  4 10
  1. Create function = x**2+1 & apply function to vectors ‘u’ & ‘v’
m <- function(x) return(x**2+1)
m(u)
## [1] 26  5 65
m(v)
## [1]  2 10 82
  1. Create function = x^2-1 & apply function to vectors ‘u’ & ‘v’
m1 <- function(x) return(x^2-1)
m1(u)
## [1] 24  3 63
m1(v)
## [1]  0  8 80
  1. Creat vector ‘y’ & use rounding function to the nearest integer, save in ‘z’ & show ‘z’:
y <- c(1.2,3.9,0.4)
z <- round(y)
z
## [1] 1 4 0
  1. Creat vector ‘r’ & use rounding function to the nearest integer, save in ‘r1’ & show ‘r1’:
r <- c(0.25,0.9,2.1,4.7,5.1)
r1 <- round(r)
r1
## [1] 0 1 2 5 5
  1. Create vectorized function with scalar arguments.
f <- function(x,c) return((x+c)^2)
f(1:3, 0)
## [1] 1 4 9
f(1:3, 1)
## [1]  4  9 16
  1. Create vectorized function with scalar arguments.
f1 <- function(x,c) return((x-c)^2)
f1(1:3, 0)
## [1] 1 4 9
f1(1:3, 1)
## [1] 0 1 4
  1. Create function; add vectors ‘x’ & ‘x2’; perform function and show results; modify result to view as 2 columns
z12 <- function(z) return(c(z,z^2))
x <- 1:8
z12(x)
##  [1]  1  2  3  4  5  6  7  8  1  4  9 16 25 36 49 64
x2 <- 1:5
z12(x2)
##  [1]  1  2  3  4  5  1  4  9 16 25
matrix(z12(x),ncol=2)
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    4
## [3,]    3    9
## [4,]    4   16
## [5,]    5   25
## [6,]    6   36
## [7,]    7   49
## [8,]    8   64
matrix(z12(x2),ncol=2)
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    4
## [3,]    3    9
## [4,]    4   16
## [5,]    5   25
  1. Create vector ‘x’ with NA value => mean calculated as NA; exclude NA value to calculate mean correctly; the NA issue doesn’t occur if NULL is used since R reads it as string of characters
x <- c(88,NA,12,168,13)
x
## [1]  88  NA  12 168  13
mean(x)
## [1] NA
mean(x,na.rm=T)
## [1] 70.25
x <- c(88,NULL,12,168,13)
mean(x)
## [1] 70.25
  1. Create vector ‘x’ and determine data characteristics
x <- c(5,NA,12)
mode(x[1])
## [1] "numeric"
mode(x[2])
## [1] "numeric"
  1. Create vector ‘y’ and determine data characteristics
y <- c("abc","def",NA)
mode(y[2])
## [1] "character"
mode(y[3])
## [1] "character"