Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



CS544 Module 1 Assignment

 

The following sample data shows the scores of the students in an exam: 58, 46, 50, 90, 42, 52, 62, 44, 96, 92, 54, 82

Do the following using R code with only a single expression for each case. The solutions should be generic and work for any size data. You can assume there will be an even number of values in the given data.

 

Q1 (25 points)

a) Assign the above data as a vector in the same order to the variable scores. Use this variable for the remaining problems.

b) Using the length function, compute how many students took the exam? Store the expression in the variable n.

c) Using indexing, write the expression for accessing the first two items. Store the expression in the variable first_and_second.

d) Using indexing, write the expression for accessing the first and last items. Store the expression in the variable first_and_last.

e) Using indexing, write the expression for accessing the middle two items. Store the expression in the variable middle_two.

 

Sample output:

[1] 12

[1] 58 46

[1] 58 82

[1] 52 62

 

Q2 (25 points)

a) Use median(scores) to compute the median of the data. Store the expression in the variable

median_score.

b) Using comparison operators, write the R expression for scores less than or equal to the median of the data. Store the expression in the variable below_median.

c) Using comparison operators, write the R expression for scores greater than the median of the data. Store the expression in the variable above_median.

d) Using the sum function, write the R expression for the number of scores less than or equal to the median of the data. Store the expression in the variable count_below_median.

e) Using the sum function, write the R expression for the number of scores greater than the median of the data. Store the expression in the variable count_above_median.

 

Sample output:

[1] 56

[1] FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE

[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE

[1] 6

[1] 6


 

Q3 (10 points)

a) Using logical indexing and the results from Q2), write the R expression for all the scores that are less than or equal to the median value of the data. Store the expression in the variable scores_below_median.

b) Similarly, write the R expression for all the scores that are greater than the median. Store the expression in the variable scores_above_median.

 

Sample output:

[1] 46 50 42 52 44 54

[1] 58 90 62 96 92 82

 

Q4 (10 points)

a) Using numeric indexing, write the R expression for the odd indexed values from the scores. Store the expression in the variable odd_index_values.

b) Similarly, write the R expression for the even indexed values from the scores. Store the expression in the variable even_index_values.

 

Use the seq function to generate the numeric indices for the above.

 

Sample output:

[1] 58 50 42 62 96 54

[1] 46 90 52 44 92 82

 

Q5 (10 points)

a) Using the paste function with LETTERS, write the expression for the following output. Store the expression in the variable format_scores_version1.

You can assume there are no more than 26 values.

Sample output:

[1] "A=58" "B=46" "C=50" "D=90" "E=42" "F=52" "G=62" "H=44" "I=96" "J=92" "K=54" "L=82"

 

b) Similarly, using the paste function with LETTERS, write the expression for the following output. Store the expression in the variable format_scores_version2.

Sample output:

[1] "L=58" "K=46" "J=50" "I=90" "H=42" "G=52" "F=62" "E=44" "D=96" "C=92" "B=54" "A=82"

 

 

Q6 (10 points)

a) Create a matrix with two rows using the scores data. The first half of the values belong to the first row of the matrix. Store the expression in the variable scores_matrix.

The code should work for any size input data.

You can assume that there are even number of values in scores.

Sample output:


 

b) 

Write the expression for displaying the first and last columns of the above matrix. The code should work for any size matrix. Store the expression in the variable first_and_last_version1. Sample output:

 

Q7 (10 points)

a) Copy scores_matrix to the variable named_matrix.

Assign column names for the named_matrix as Student_1, Student_2,… and row names as Quiz_1, Quiz_2, ... The code should work for any size matrix, i.e., for any number of columns in the matrix and any number of rows.

Sample output:

 

b) Show the result for displaying the first and last columns of the named_matrix. The code should work for any size matrix. Store the expression in the variable first_and_last_version2. Sample output:

 

 

Submission:

• You must work on your assignments individually. You are not allowed to copy the answers from the others.

• Each assignment has a strict deadline. Please plan accordingly to submit on time.

Provide all R code in a single file, CS544_HW1_LastName_Last4DigitsBUID.R.

For example, if you are John Doe with BU ID as U12-34-0590, the file name should be

CS544_HW1_Doe_0590.R

 

Upload the R file to the Assignments section of Blackboard.