Problems

Q1

n_dims <- sample(3:10,1,replace = TRUE)
rep(n_dims)
## [1] 5
n_dims <- sample(3:10,1,replace = TRUE)
# single random integer between 3 and 10
# different function for integer values, whole numbers
# replace true means re sample it the number changes

n_dims2 <- 1:n_dims^2
# create a vector of consecutive integers from 1 to n_dims^2


n_dims3 <- sample(n_dims2)
# use sample function to randomly reshuffle values

m <- matrix(data=n_dims3,nrow= n_dims, ncol = n_dims)
# create a square matrix with these elements

print(m)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   23    5   20   14   25
## [2,]   10    8    9   21    6
## [3,]   16   17    3   22   11
## [4,]   15    1   13    7   24
## [5,]    2   19    4   12   18
# print out the matrix
# Notes: Long list of values

tmatrix <- t(m)
# find a function in r to transpose matrix

print(tmatrix)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   23   10   16   15    2
## [2,]    5    8   17    1   19
## [3,]   20    9    3   13    4
## [4,]   14   21   22    7   12
## [5,]   25    6   11   24   18
# print it out again and note how it has changed
#flips it, so if a number is at position 1,3 it will be moves to 3,1
# if a number is at 3,3 it will stay

x <- sum(tmatrix[1,])
y <- mean(tmatrix[1,])
# Sum and mean of 1st row

x2 <- sum(tmatrix[length(n_dims),])
y2 <- mean(tmatrix[length(n_dims),])
# Sum and mean of last row
# calculate the sum and the mean of the elements in 1st and last row


answer <- eigen(tmatrix)
# eigen() function in matrix


values <- answer$values
answer <- answer$vectors
# $values and $vectors in output



typeof(values)
## [1] "complex"
typeof(answer)
## [1] "complex"
# type is double
# typeof() function to figure out type

Q2

my_matrix <- matrix(sample(1:10, 16, replace = TRUE), nrow = 4)
# my_matrix


my_setLogical <- sample(1:20,100, replace = TRUE)
x <- my_setLogical > 10
#my_logical


my_letters <- sample(letters)
#my_letters

newlist <- rbind(my_matrix[2,2],my_setLogical[2],my_letters[2])
#newList

typeof(newlist)
## [1] "character"
# is a character
#typeof()

y <- c(newlist)
typeof(y)
## [1] "character"
# it is a character
#combine

Q3

Create a data frame with the two variables (= columns) and 26 cases (= rows) below:

my_unis <- sample(0:10,26,replace = TRUE)
my_letters <- sample(LETTERS)
rep(my_unis)
##  [1]  2  7  3  6  0  7 10  3  0  3  5  7  0  1  0  1  1  6  3  5  3  1  8  0  7
## [26]  5
datax <- data.frame(my_unis, my_letters)
#Data frame
rep(datax)
## $my_unis
##  [1]  2  7  3  6  0  7 10  3  0  3  5  7  0  1  0  1  1  6  3  5  3  1  8  0  7
## [26]  5
## 
## $my_letters
##  [1] "X" "Z" "B" "G" "J" "P" "U" "W" "L" "K" "O" "D" "E" "I" "H" "Y" "T" "Q" "V"
## [20] "S" "N" "M" "R" "F" "C" "A"
datax$my_unis[sample(1:26, 4)] <- NA
#first variable

missing <- which(is.na(datax$my_unis))
#missing row code
print(missing)
## [1]  2 13 16 20
datanew <- datax[order(datax$my_letters), ]
#Reorder data frame for second variable in alphabetical order
rep(datanew)
## $my_unis
##  [1]  5  3  7  7 NA  0  6  0  1  0  3  0  1  3  5  7  6  8 NA  1 10  3  3  2 NA
## [26] NA
## 
## $my_letters
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
## [20] "T" "U" "V" "W" "X" "Y" "Z"
meancolumn <- mean(datax$my_unis, na.rm = TRUE)
# Calc column mean for first variable
rep(meancolumn)
## [1] 3.681818

Return Home