Vector

 Code:

Vectors

x <- 10
typeof(x)
x <- 10.25
typeof(x)
x <- 'Hello'
typeof(x)
x <- "hello"
typeof(x)
x <- TRUE
typeof(x)

x <- c(10, 20, 30, 40, 50)
x
typeof(x)
assign('y', c(10, 20, 30, 40, 50))
y
length(x)

x <- c(10.25,3.5,8.75)
x
x <- c('a','h','j')
x
x <- c('aaa','bbb','ccc')
x
x <- c("a","h","j")
x
x <- c(TRUE,TRUE,FALSE,TRUE)
x
x <- c(T,F,F)
x
x <- 3+4i
x

y <- c(1,2,3,4,5,6,7,8,9,10)
y
x <- 1:10  # : Sequence operator
x

x <- c(10, 20, 30, 40, 50)
x
y <- c(60.5,70.6,80)
y
x = c(x,y)
x

y <- c('aaa','bbb')
y
x = c(x,y)
x

z <- y <- x <- c(10,30,50)
x
y
z

x <- vector()
x

x <- vector('numeric', length=5)
x
x <- vector('integer', length=5)
x
x <- vector('logical', length=5)
x
x <- vector('character', length=5)
x
x <- vector('complex', length=5)
x
x <- vector('raw', length=5)
x

length(x)

Vector Index


x <- c(10,45,30,50,35,40,80,25)
x

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

x <- c(10, 20, 30, 40, 50)
x
x[1]
x[5]
x[10]

x[3]<-35
x
x[10]<-45
x
x[-3]<-45
x

x <- c(10, 20, 30, 40, 50)
x
x[1:3]
x[2:4]
x[1:10]


y <- c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE) 
x[y]
y <- c(TRUE, TRUE, FALSE, TRUE) #Round Robin
x[y]

x <- letters
x
x[5:12]
x <- LETTERS
x
x[15:26]
x <- month.name
x
x[3:6]
x <- month.abb
x
x[9:12]


x <- c('a', 'b', 'c', 'd', 'e') 
for(i in 1:5) {
  print(x[i])   #prints every element of the vector
}

x <- c('a', 'b', 'c', 'd', 'e') 
for(i in 1:8) {
  print(x[i])   #index is beyond length prints NA
}

for(i in seq_along(x))
  print(x[i])

for(i in x) {
  print(i)   #prints every element of the list
}

for(i in x) print(i)   #prints every element of the list

x <- matrix(1:6, 2, 3)
for(i in seq_len(nrow(x))) {
  for(j in seq_len(ncol(x))) {
    print(x[i, j])
  }
}

Matching Operator

x <- c(1,2,3,4,5)
2%in%x 

Arithmetic on Vectors

x <- c(10,45,30,50,35,40,80,25)
x
1/x
x + 1
x * 2
x ^ 2
sqrt(x)
sqrt(-4) #Error or Warning NaN no result
sqrt(-4+0i)
sqrt(4+0i)

y <- c(1,4,3,5,3,4,8,2)
z <- x + y
z

y <- c(1,4,3,5,3) #Shorter length
z <- x + y
z

length(x)
rev(x)
sum(x)
prod(x)

sort(x)
sort(x,decreasing=TRUE)


x <- c(1,4,3,5,3)
y <- c(2,1,4,3,8)
x%*%y  #dot product of vectors
crossprod(x,y)

x%o%y  #outer product of vectors
tcrossprod(x,y)

Implicit Explicit Coercion


z <- c(10,'John',20,30)
z

z <- c(10,'John',20.75,30)
z

z <- c(10,'John',20.75,TRUE)
z

z <- c(10,TRUE)
z
z <- c(10,FALSE)
z

z <- c('John',TRUE)
z

x <- 0:5
x
class(x)
as.numeric(x)
as.logical(x)
as.character(x)

x <- c('John','Jack','Bob')
x
class(x)
as.character(x)
as.numeric(x)
as.logical(x)

Logical Vectors 


x <- c(10,45,30,50,35,40,80,25)
x

y <- x > 30
y

y <- x > 30 & x <60
y

x <- c(10,45,30,50)
y <- c(20,15,25,65)
x < y

as.numeric(x > 30 & x <60)
sum(x > 30 & x <60)
which(x > 30 & x <60) #Returns the indexes
x[which(x > 30 & x <60)] #Returns the elements
sum(x[which(x > 30 & x <60)])

max(x)
which.max(x)

Factor

x <- factor(c('Male', 'Male', 'Female', 'Male', 'Female', 'Male'))
x
table(x)

Maths


x <- c(4.258,-3.853,5.457,7.504)
abs(x)
x <- c(4.258,3.853,5.457,7.504)
ceiling(x)
floor(x)
round(x, 2)
round(x)
trunc(x)

x <- c(16,36,30,81,25)
sqrt(x)
exp(x)
x^2
log(x)
log(x, base=2)
log2(x)
log(x, base=10)
log10(x)


x <- c(4,3,5,7,10)
factorial(x)

Random Numbers

x <- rnorm(10) 
x

x <- rnorm(10, mean = 0, sd = 1)
x

x <- rnorm(10, 20, 2) 
x

x <- rnorm(10, mean=20, sd=2)  # Same as above
x

set.seed(1)
x <- rnorm(5)
x
set.seed(1)
x <- rnorm(5)
x

Comments

Popular posts from this blog

Probability: Binomial Distribution

Probability: Normal Distribution

Hypothesis testing