R Practice Questions for Matrix & Data Frames
1) Define a Matrix with names of the Columns & Rows defined at the time of Matrix definition
2) Do a row bind of a matrix(1:10 , 2 , 5 ) and another matrix( 11:15 , 1 ,5)
3) Create three vectors x,y,z with integers and each vector has 3 elements.
3.a ) Combine the three vectors to become a 3×3 matrix 'A' where each column represents a vector.
3.b) Change the row names to a,b,c.
4) Show that the data structure created in the previous question is of 'Matrix' type
5) Create a vector with 12 integers. Convert the vector to a 4*3 matrix 'B' using matrix().
5.a) Please change the column names to x, y, z & row names to a, b, c, d.
5.b) Also ensure that values are populated at 'Row' levels instead of 'Column' levels.
6) There exist a predefined 'Character Vector' in R called LETTERS ,
this predefined variable LETTERS contain all the 26 English alphabets in Capital .
6.a) Assign this predefined variable LETTERS to a new user defined variable 'Alpha_Mtrx'
6.b) show the type of Data Structure for this new variable Alpha_Mtrx
6.c) show the dimension of this new variable Alpha_Mtrx
6.d) now use the 'dim()' function to change the dimension of Alpha_Mtrx to have 2 rows & 13 columns
6.e) now show that the variable Alpha_Mtrx is a Matrix variable by using the function is.xxxxxx()
6.f) now change the names of the rows in the matrix to 'Row_1' & 'Row_2'
7) create a numeric matrix Num_Mtrx having values 1:20 with 4 rows & 5 columns.
7.a) then create a new matrix from Num_Mtrx where rows in Num_Mtrx gets converted into columns
8) from the above numeric matrix Num_Mtrx ,
8.a) create a numeric vector containing 5 elements where each element is sum of values in each column respectively
8.b) create a numeric vector containing 4 elements where each element is sum of values in each row respectively
9) Add 10 to each numeric element in the Num_Mtrx and have the output stored in a new matrix variable
10) Flatten the Matrix Num_Mtrx into a 1 dimensional numeric vector
11) extract from the Matrix Num_Mtrx , only the rows 2nd & 4th and store it in new Matrix
12) extract from the Matrix Num_Mtrx , only the columns 1st, 2nd & 4th and store it in new Matrix
13) extract from the Matrix Num_Mtrx , only the values in row 2 & columns 3rd & 5th.
14) can you create Data Frame from the matrix variable Num_Mtrx_DF
15) use the predefined character vector 'LETTERS' containing all English alphabets
15.a) create a Data Frame named Letters_DF where it contains 2 columns
15.b) where the first column is named as 'Even_Alphabets' & second column as 'Odd_Alphabets'
15.c) and column 1 contains all the alphabets in even position ( 2nd , 4th , 6th .....)
15.d) and column 2 contains all the alphabets in odd position ( 1st , 3rd , 5th .....)
15.e) now extract the elements in the even rows of the column named 'Even_Alphabets' in the data frame created above
15.f) now swap the position of the columns in the Dataframe ,1st column becomes 2nd column & vice versa
16) Create the dataframes to merge:
buildings <- data.frame(location=c(1, 2, 3), name=c("building1", "building2", "building3"))
data <- data.frame(survey=c(1,1,1,2,2,2), location=c(1,2,3,2,3,1), efficiency=c(51,64,70,71,80,58))
The dataframes, buildings and data have a common key variable called, “location”.
Use the merge() function to merge the two dataframes by “location”, into a new dataframe, “buildingStats”.
Thank You & Happy Learning