#Homewrok5

Nicolás Zapata

1

Assign to the variable n_dims a single random integer between 3 and 10.

n_dims <- sample(3:10, size=1)
print(n_dims)
## [1] 9

Create a vector of consecutive integers from 1 to n_dims2

my_vector <- seq(1:n_dims^2)

Use the sample function to randomly reshuffle these values.

matrix1 <- matrix(sample(my_vector), nrow=n_dims)
# print out the matrix
print(matrix1) 
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]   78    4   46    8   36   68   51   35   72
##  [2,]   40   50    7   10    2   14   71   52   44
##  [3,]   76   12   21    5   11   20    6   70   25
##  [4,]   64   60   39   23   57   15   61   66   77
##  [5,]   54   45   41   30   63   48   81   69   58
##  [6,]   42   37   28   55   32   34   67   17   24
##  [7,]    3   26    1   33    9   49   29   56   79
##  [8,]   38   27   16   62   13   74   65   22   59
##  [9,]   43   31   18   53   73   80   47   75   19

Find a function in r to transpose the matrix

transposed_matrix1 <- t(matrix1)
print(transposed_matrix1)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]   78   40   76   64   54   42    3   38   43
##  [2,]    4   50   12   60   45   37   26   27   31
##  [3,]   46    7   21   39   41   28    1   16   18
##  [4,]    8   10    5   23   30   55   33   62   53
##  [5,]   36    2   11   57   63   32    9   13   73
##  [6,]   68   14   20   15   48   34   49   74   80
##  [7,]   51   71    6   61   81   67   29   65   47
##  [8,]   35   52   70   66   69   17   56   22   75
##  [9,]   72   44   25   77   58   24   79   59   19

Calculate the sum and the mean of the elements in the first row and then the last row.

# mean first row
firstrow_mean <- mean(matrix1[1,])
print(firstrow_mean) 
## [1] 44.22222
# sum  first row
firstrow_sum <- sum(matrix1[1,])
print(firstrow_sum)
## [1] 398
# mean last row 
lastrow_mean <- mean(matrix1[n_dims,])
print(lastrow_mean)
## [1] 48.77778
# sum last row
lastrow_sum <- sum(matrix1[n_dims,]) 
print(lastrow_sum)
## [1] 439

Read about the eigen() function and use it on your matrix

matrix_eigen <- eigen(matrix1) 
print(matrix_eigen)
## eigen() decomposition
## $values
## [1] 371.7380277+ 0.00000i  71.1977128+ 0.00000i -18.9569043+54.76163i
## [4] -18.9569043-54.76163i -33.4425473+32.25510i -33.4425473-32.25510i
## [7]   0.2367961+17.15548i   0.2367961-17.15548i   0.3895706+ 0.00000i
## 
## $vectors
##               [,1]            [,2]                     [,3]
##  [1,] 0.3506800+0i -0.596545683+0i -0.05884473-0.229956272i
##  [2,] 0.2417436+0i  0.349817183+0i -0.50108748+0.000000000i
##  [3,] 0.2191546+0i -0.591763068+0i -0.21616899+0.009436224i
##  [4,] 0.4088426+0i -0.039941452+0i -0.20091008+0.183333698i
##  [5,] 0.4277468+0i  0.056168998+0i -0.17531472+0.105079056i
##  [6,] 0.2918612+0i  0.006997265+0i -0.04601784+0.336945052i
##  [7,] 0.2600577+0i  0.391369526+0i  0.25835090-0.415410520i
##  [8,] 0.3321607+0i  0.100958690+0i  0.19267888-0.088283166i
##  [9,] 0.3966000+0i  0.058443627+0i  0.29686767+0.204908573i
##                           [,4]                    [,5]                    [,6]
##  [1,] -0.05884473+0.229956272i -0.23276894+0.15891561i -0.23276894-0.15891561i
##  [2,] -0.50108748+0.000000000i  0.36902796+0.22251688i  0.36902796-0.22251688i
##  [3,] -0.21616899-0.009436224i  0.23472242-0.10200966i  0.23472242+0.10200966i
##  [4,] -0.20091008-0.183333698i -0.08558977+0.02293721i -0.08558977-0.02293721i
##  [5,] -0.17531472-0.105079056i  0.11010276+0.03040861i  0.11010276-0.03040861i
##  [6,] -0.04601784-0.336945052i  0.32324073+0.07026102i  0.32324073-0.07026102i
##  [7,]  0.25835090+0.415410520i -0.54766446+0.00000000i -0.54766446+0.00000000i
##  [8,]  0.19267888+0.088283166i -0.07056112+0.10477936i -0.07056112-0.10477936i
##  [9,]  0.29686767-0.204908573i  0.19003273-0.43248339i  0.19003273+0.43248339i
##                          [,7]                    [,8]           [,9]
##  [1,]  0.29232776-0.14786118i  0.29232776+0.14786118i  0.16643969+0i
##  [2,] -0.30428333-0.01301204i -0.30428333+0.01301204i -0.58519547+0i
##  [3,] -0.69884284+0.00000000i -0.69884284+0.00000000i -0.55488169+0i
##  [4,]  0.07632344-0.17321553i  0.07632344+0.17321553i  0.19312256+0i
##  [5,]  0.30085302+0.20969191i  0.30085302-0.20969191i  0.26060311+0i
##  [6,] -0.21081844+0.09368379i -0.21081844-0.09368379i -0.33991183+0i
##  [7,]  0.11197098+0.03669253i  0.11197098-0.03669253i  0.26775044+0i
##  [8,] -0.13477234-0.08435733i -0.13477234+0.08435733i  0.04301394+0i
##  [9,]  0.20929251+0.07101023i  0.20929251-0.07101023i  0.16631160+0i

Dig in with the typeof() function to figure out their type.

typeof(matrix_eigen$values)
## [1] "complex"
typeof(matrix_eigen$vectors)
## [1] "complex"

2

Create a list with the following named elements:

# my_matrix, which is a 4 x 4 matrix filled with random uniform values

my_matrix <- matrix(data=runif(16), nrow=4)
print(my_matrix) 
##           [,1]      [,2]       [,3]      [,4]
## [1,] 0.7449822 0.7315595 0.26298995 0.3767629
## [2,] 0.4400170 0.4839849 0.34148083 0.5623074
## [3,] 0.1150394 0.1710480 0.21024554 0.6799846
## [4,] 0.6756428 0.6758485 0.01631959 0.7453888
# my_logical which is a 100-element vector of TRUE or FALSE values. Do this efficiently by setting up a vector of random values and then applying an inequality to it.
my_logical <- (runif(100) > .8)
print(my_logical) 
##   [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [13] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [25]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [37]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [49] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
##  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [97] FALSE  TRUE FALSE FALSE
# my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters)
print(my_letters)
##  [1] "p" "r" "q" "g" "d" "x" "s" "m" "c" "z" "w" "a" "i" "e" "b" "f" "u" "h" "v"
## [20] "o" "l" "t" "k" "j" "n" "y"
# create a new list, which has the element[2,2] from the matrix, the second element of the logical vector, and the second element of the letters vector
my_list1 <- list(my_matrix, my_logical, my_letters)

new_list <- list(my_matrix[2,2], my_logical [2], my_letters[2]) 
print(new_list)
## [[1]]
## [1] 0.4839849
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "r"
# use the typeof() function to confirm the underlying data types of each component in this list
typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"
# combine the underlying elements from the new list into a single atomic vector with the c() function
combined_elements_vector <- c(new_list[[1]], new_list[[2]], new_list[[3]])
print(combined_elements_vector)
## [1] "0.483984902733937" "FALSE"             "r"
# what is the data type of this vector?
typeof(combined_elements_vector)
## [1] "character"

3

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

#call the first variable my_unis and fill it with 26 random uniform values from 0 to 10

# call the second variable my_letters and fill it with 26 capital letters in random order.
dataframe <- data.frame(my_unis = runif(26, min=0, max=10), my_letters = sample(LETTERS))
print(dataframe)
##      my_unis my_letters
## 1  9.6384166          S
## 2  0.0126048          P
## 3  7.0752621          R
## 4  6.3018760          E
## 5  7.7307804          A
## 6  8.9260688          B
## 7  5.1120408          W
## 8  7.4900107          N
## 9  9.2637980          H
## 10 0.9175352          T
## 11 4.9665549          D
## 12 1.9750126          V
## 13 9.9413290          Y
## 14 0.3682104          L
## 15 2.1756307          O
## 16 8.9093614          F
## 17 1.1893646          X
## 18 3.6903899          I
## 19 1.6237643          Z
## 20 1.6669152          K
## 21 9.7250471          Q
## 22 8.1039974          U
## 23 9.5332763          J
## 24 3.9991053          M
## 25 8.5224732          G
## 26 0.6452286          C
# for the first variable, use a single line of code in R to select 4 random rows and replace the numerical values in those rows with NA.
dataframe[sample(1:26, size=4), 1] <- NA
# for the first variable, write a single line of R code to identify which rows have the missing values
which(is.na(dataframe$my_unis))
## [1] 12 13 17 21
# re-order the entire data frame to arrange the second variable in alphabetical order
dataframe <- dataframe[order(dataframe$my_letters),]
print(dataframe)
##      my_unis my_letters
## 5  7.7307804          A
## 6  8.9260688          B
## 26 0.6452286          C
## 11 4.9665549          D
## 4  6.3018760          E
## 16 8.9093614          F
## 25 8.5224732          G
## 9  9.2637980          H
## 18 3.6903899          I
## 23 9.5332763          J
## 20 1.6669152          K
## 14 0.3682104          L
## 24 3.9991053          M
## 8  7.4900107          N
## 15 2.1756307          O
## 2  0.0126048          P
## 21        NA          Q
## 3  7.0752621          R
## 1  9.6384166          S
## 10 0.9175352          T
## 22 8.1039974          U
## 12        NA          V
## 7  5.1120408          W
## 17        NA          X
## 13        NA          Y
## 19 1.6237643          Z
# calculate the column mean for the first variable
mean(dataframe$my_unis, na.rm=TRUE)
## [1] 5.303332