#1 Create a vector called scores containing the values: 72, 85, 90, 66, 88, 91, 70
scores<-c(72, 85, 90, 66, 88, 91, 70) #this stores the items/scores into the vector
scores #this allows us to print the vector and all its contained scores
## [1] 72 85 90 66 88 91 70
#1A - What is the length of the scores vector?
length(scores) #this tells us that 'scores' vector has a sample size of 7 (contains 7 elements)
## [1] 7
#1B - Extract the 2nd, 4th, and 7th elements of the vector
scores[2] #this tells us the 2nd element of this vector; which is 85
## [1] 85
scores[4] #this tells us the 4th element of this vector; which is 66
## [1] 66
scores[7] #this tells us the 7th element of this vector; which is 70
## [1] 70
#1C - Find all values that are greater than 80 in this vector
scores[scores>80] #this produces all of the numbers in the vector that are larger than 80: this is 85, 90, 88, and 91.
## [1] 85 90 88 91
#2 Use runif() function to generate 60 random numbers between 10 and 500 and create a 10 by 6 matrix using these numbers (fill by rows).
matrix <- array(runif(60, min=10, max=500),
dim=c(10, 6)) #this generates 60 random numbers between 10 and 500 and places it in a matrix that is 10 rows x 6 columns.
matrix #this prints the matrix that is 10 rows x 6 columns (with the random numbers mentioned above)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 401.2989 490.11983 299.7342 297.07816 317.3799 118.9281
## [2,] 106.0653 365.90899 366.3137 154.98757 200.8533 314.4396
## [3,] 494.0830 156.53419 48.1419 126.59012 364.8811 167.9471
## [4,] 109.5470 417.67201 417.0282 225.53518 191.7576 186.2305
## [5,] 253.2713 477.99222 404.7836 52.83130 458.9395 73.8057
## [6,] 383.8379 219.76204 297.0308 412.38416 189.0406 473.8565
## [7,] 217.9277 390.69711 185.5878 321.61113 490.7745 115.9661
## [8,] 264.3394 286.78238 296.0974 368.41176 396.1447 293.3660
## [9,] 416.6676 424.51824 242.3561 497.22888 306.5413 437.0299
## [10,] 126.4289 65.22194 486.9922 64.18233 133.0974 346.4584
#2A - Extract columns 2 and 5 as a new matrix.
matrix_new<- matrix[, c(2, 5)] #this specifically takes out the 2nd and 5th column from the original matrix and stores it into a new matrix that is 10 rows by 2 columns.
matrix_new #this prints the new matrix that is only columns 2 & 5 from the original matrix.
## [,1] [,2]
## [1,] 490.11983 317.3799
## [2,] 365.90899 200.8533
## [3,] 156.53419 364.8811
## [4,] 417.67201 191.7576
## [5,] 477.99222 458.9395
## [6,] 219.76204 189.0406
## [7,] 390.69711 490.7745
## [8,] 286.78238 396.1447
## [9,] 424.51824 306.5413
## [10,] 65.22194 133.0974
#2B - Add up columns 1 & 2
column_3<-matrix_new[,1] + matrix_new[,2] #this creates a new vector that is the combination of values present in both columns from the "new matrix"
updated_matrix<-cbind(matrix_new,column_3) #this combines the "column 3" vector into the "new matrix" as a new column
updated_matrix #this prints the updated matrix where "column 3" is the sum of variables from the first two columns
## column_3
## [1,] 490.11983 317.3799 807.4998
## [2,] 365.90899 200.8533 566.7623
## [3,] 156.53419 364.8811 521.4153
## [4,] 417.67201 191.7576 609.4296
## [5,] 477.99222 458.9395 936.9317
## [6,] 219.76204 189.0406 408.8027
## [7,] 390.69711 490.7745 881.4716
## [8,] 286.78238 396.1447 682.9271
## [9,] 424.51824 306.5413 731.0596
## [10,] 65.22194 133.0974 198.3194
#2C - Identify all rows where the value in column 3 is greater than 250.
which(updated_matrix[, "column_3"] > 250) #this specifically identifies which rows from the updated matrix have values that are greater than 250 from the "updated matrix"
## [1] 1 2 3 4 5 6 7 8 9
matrix_250<-updated_matrix[updated_matrix[,"column_3"] >250,] #this creates a matrix that only includes rows where the value in column 3 is larger than 250
matrix_250 #this prints the matrix where all values from column 3 are larger than 250.
## column_3
## [1,] 490.1198 317.3799 807.4998
## [2,] 365.9090 200.8533 566.7623
## [3,] 156.5342 364.8811 521.4153
## [4,] 417.6720 191.7576 609.4296
## [5,] 477.9922 458.9395 936.9317
## [6,] 219.7620 189.0406 408.8027
## [7,] 390.6971 490.7745 881.4716
## [8,] 286.7824 396.1447 682.9271
## [9,] 424.5182 306.5413 731.0596
#3) Create the following dataframes
###3 Creates the matrix with names, age, height, and weight (Matrix A)
Age<-c(25, 31, 23, 52, 76, 49, 26) #stores all of the age values in one vector
Height<-c(177, 163, 190, 179, 163, 183, 164) #stores all of the height values in one vector
Weight<-c(57, 69, 83, 75, 70, 83, 53) #stores all of the weight values in one factor
matrix_A<-cbind(Age, Height, Weight) #combines the "age", "height", and "weight" vectors into a matrix where each act as a column
rownames(matrix_A)<-c("Alex", "Lilly", "Mark", "Oliver", "Martha", "Lucas", "Caroline") #rename the row numbers into names
print(matrix_A) #this is the matrix that contains the names of people in rows with their age, height, and weight as columns
## Age Height Weight
## Alex 25 177 57
## Lilly 31 163 69
## Mark 23 190 83
## Oliver 52 179 75
## Martha 76 163 70
## Lucas 49 183 83
## Caroline 26 164 53
###3 Creates the matrix with names and whether they are working (Matrix B)
Working<-c("Yes", "No", "No", "Yes", "Yes", "No", "Yes") #stores the employment information for each person as a vector
matrix_B<-data.frame(Working) #turns the vector into one column dataframe
rownames(matrix_B)<-c("Alex", "Lilly", "Mark", "Oliver", "Martha", "Lucas", "Caroline") #adds the names of people as row names into this matrix
matrix_B #prints the Matrix with names of people as row names and their employment status as a column
#3A: combining dataframes
matrix_final<-cbind(matrix_A, matrix_B) #combines the two matrices as mentioned above into one matrix. It keeps the row names from matrix A, which are the names of the people — which is also present in Matrix B
print(matrix_final) #prints the matrix with names of people as row names, their age, height, weight, and employment as columns.
## Age Height Weight Working
## Alex 25 177 57 Yes
## Lilly 31 163 69 No
## Mark 23 190 83 No
## Oliver 52 179 75 Yes
## Martha 76 163 70 Yes
## Lucas 49 183 83 No
## Caroline 26 164 53 Yes
#3B: What class of data is in each column of the combined data frame?
class(matrix_final$Age) #this tells us that "age" is comprised of numeric values since age is conveyed as number of years
## [1] "numeric"
class(matrix_final$Height) #this tells us that "height" is comprised of numeric values since height is conveyed as number of inches/cms of the individual
## [1] "numeric"
class(matrix_final$Weight) #this tells us that "weight" is comprised of numeric values since weight is conveyed as a number of pounds
## [1] "numeric"
class(matrix_final$Working) #this tells us that "working" is made up of character data because it is made of characters like letters/words
## [1] "character"
#3C: Replace row names with letters from A to G.
rownames(matrix_final)<-c("A", "B", "C", "D", "E", "F", "G") #renames the row names from people names into the letters A-G
print(matrix_final) #prints Matrix with letters A-G instead of people names for row na,es
## Age Height Weight Working
## A 25 177 57 Yes
## B 31 163 69 No
## C 23 190 83 No
## D 52 179 75 Yes
## E 76 163 70 Yes
## F 49 183 83 No
## G 26 164 53 Yes