#Create one random integer number between 3 and 10
n_dims <- as.integer(runif(1,min=3,max=10))
typeof(n_dims)
## [1] "integer"
print(n_dims)
## [1] 7
#Creates a new vector between 1 and the random generated number from before
new_vec<-(1:(n_dims^2))
print(new_vec)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#Reshuffles last vector
sample(x=new_vec)
## [1] 44 36 32 4 45 48 35 49 2 31 33 17 5 14 40 18 42 23 1 26 39 25 13 12 21
## [26] 16 43 30 28 46 27 3 15 10 41 22 11 34 20 47 29 8 24 19 7 9 38 37 6
#Create a matrix with the last vect. Is a square matrix, because nrow=n_dims
m<-matrix(data = new_vec, nrow = n_dims)
print(m)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 8 15 22 29 36 43
## [2,] 2 9 16 23 30 37 44
## [3,] 3 10 17 24 31 38 45
## [4,] 4 11 18 25 32 39 46
## [5,] 5 12 19 26 33 40 47
## [6,] 6 13 20 27 34 41 48
## [7,] 7 14 21 28 35 42 49
#Transpose with t()
m_matrix<-t(m)
print(m_matrix)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 2 3 4 5 6 7
## [2,] 8 9 10 11 12 13 14
## [3,] 15 16 17 18 19 20 21
## [4,] 22 23 24 25 26 27 28
## [5,] 29 30 31 32 33 34 35
## [6,] 36 37 38 39 40 41 42
## [7,] 43 44 45 46 47 48 49
#Sum and mean teh first row
print(sum(m_matrix[1,]))
## [1] 28
print(mean(m_matrix[1,]))
## [1] 4
#Sum and mean the very last row.
print(sum(m_matrix[n=n_dims,]))
## [1] 322
print(mean(m_matrix[n=n_dims,]))
## [1] 46
#Coverts the matrix en eigen values
m_eigen<-eigen(m_matrix, only.values = TRUE)
print(m_eigen)
## $values
## [1] 1.825171e+02+0.000000e+00i -7.517104e+00+0.000000e+00i
## [3] -5.906038e-15+0.000000e+00i 7.498614e-16+0.000000e+00i
## [5] -5.245824e-17+2.931226e-16i -5.245824e-17-2.931226e-16i
## [7] -2.103901e-16+0.000000e+00i
##
## $vectors
## NULL
typeof(m_eigen) #They are a list of variables
## [1] "list"
my_matrix<-matrix(runif(16), nrow = 4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 0.7827479 0.66270050 0.4433672 0.8991848
## [2,] 0.6243047 0.32954304 0.4755821 0.7240508
## [3,] 0.7820024 0.18485656 0.9112969 0.9251191
## [4,] 0.1556300 0.09209674 0.8186356 0.4325879
my_logical<-(runif(100)>.5)
print(my_logical)
## [1] TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE
## [13] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
## [25] TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE
## [37] TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
## [49] TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE
## [61] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE
## [73] FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE
## [85] FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
## [97] FALSE FALSE TRUE FALSE
.<-letters
my_letters<-sample(.)
print(my_letters)
## [1] "v" "g" "k" "w" "b" "r" "a" "l" "m" "i" "z" "c" "u" "j" "s" "d" "n" "p" "x"
## [20] "h" "o" "e" "y" "f" "t" "q"
Then, complete the following steps:
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. use the typeof() function to confirm the underlying data types of each component in this list combine the underlying elements from the new list into a single atomic vector with the c() function. what is the data type of this vector?
my_list<-list(my_matrix[2,2], my_logical[2], my_letters[2])
print(my_list)
## [[1]]
## [1] 0.329543
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "g"
#Typeof to verify which type of object. The [[]] are used to determine the object within the list.
typeof(my_list[[1]])#It is a double
## [1] "double"
typeof(my_list[[2]])#It is logical
## [1] "logical"
typeof(my_list[[3]])#it is a character
## [1] "character"
new_vector<-c(my_list[[1]], my_list[[2]], my_list[[3]])
typeof(new_vector) #Its a character. Because character is the one with higher herarqui in the vecttos.
## [1] "character"
print(new_vector) #Note that the number and letter are in "" becouse they are characters.
## [1] "0.329543038271368" "TRUE" "g"
#Create the variables
my_unis <- as.integer(runif(26,min=0,max=10))
my_letters <- sample(LETTERS)
#Convert in data frame
my_df <-data.frame(my_unis, my_letters)
#Will create a new data frame replacing the variable my_unis to the same variable replacing four random numbers to NA
my_df<-data.frame(my_unis=replace(my_unis, sample(my_unis, size=4), NA), my_letters)
#Print which values are missing
print(which(is.na(my_df)))
## [1] 1 4 5
#order my_df by my_letters alphabetical order
my_df2 <- my_df[order(my_df$my_letters),]
#Print the mean of my_unis of the data frame my_df, omitting NA values.
print(mean(my_df2$my_unis, na.rm = TRUE))
## [1] 4.913043
#Not committing NA values. Does not work. :(
print(mean(my_df2$my_unis, na.rm = FALSE))
## [1] NA