Question 1: “Create a vector called scores containing the values: 72,85,90,66,88,91,70. Answer the following:”

scores <- c(72, 85, 90, 66, 88, 91, 70.)

A. What is the length of scores?

length(scores)
## [1] 7

B. Extract the 2nd, 4th, and 7th elements.

scores[c(2,4,7)]
## [1] 85 66 70

C. Find all values in scores that are greater than 80.

scores[scores>80]
## [1] 85 90 88 91

Question 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). Using this matrix, answer the following:

a<-runif(60, 10, 500)
dm1<-matrix(a, nrow = 6, ncol = 10)
dm1
##           [,1]     [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
## [1,] 352.74900  88.7196 110.62568 455.74658 228.27690  73.18932 106.32917
## [2,] 246.35939 137.0152 128.46820  70.35103 132.47600  44.33329  71.85511
## [3,]  20.08998 344.6132 469.19447 220.09616  60.02196 181.01643 233.47540
## [4,] 349.16378 415.0871  25.31133  74.12617 267.21676 112.88036 374.95979
## [5,]  50.48898 267.4727  94.18515 338.07967  53.11656 264.55122 110.34175
## [6,] 379.83312 455.4018  55.82743 214.69175 225.57285 234.04814 432.21653
##           [,8]      [,9]     [,10]
## [1,] 185.54739  81.84190 387.11798
## [2,]  50.35562  25.02829 303.17765
## [3,]  35.20263 214.99250  57.27482
## [4,]  37.42097 345.39840 246.89996
## [5,] 101.68860 199.54120 425.52651
## [6,]  79.52481 297.12392 403.52598

A. Extract columns 2 and 5 as a new matrix.

d1<- dm1[,2]
d2<- dm1[,5]
dm2<-matrix(c(d1,d2),nrow = 2,ncol = 6, byrow=TRUE)
dm2
##          [,1]     [,2]      [,3]     [,4]      [,5]     [,6]
## [1,]  88.7196 137.0152 344.61321 415.0871 267.47266 455.4018
## [2,] 228.2769 132.4760  60.02196 267.2168  53.11656 225.5729

B. Add up columns 1 and 2.

d3<-dm2[,1]
d4<-dm2[,2]
d5<-sum(d3)+sum(d4)
d5
## [1] 586.4877

C. Identify all rows where the value in column 3 is greater than 250.

d6<-dm2[,3]
d7<-c(dm2[d6>250])
d7
## [1]  88.7196 137.0152 344.6132 415.0871 267.4727 455.4018

Question 3: Create the following two data frames. Answer the following:

Age<-c(25,31,23,52,76,49,26)
Height<-c(177,163,190,179,163,183,164)
Weight<-c(57,69,83,75,70,83,53)
df1<-data.frame(Age, Height, Weight)
rownames(df1)<- c("Alex","Lilly","Mark","Oliver","Martha","Lucas","Caroline")
df1
##          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
Working<-c("Yes","No","No","Yes","Yes","No","Yes")
df2<-data.frame(Working)
rownames(df2)<-c("Alex","Lilly","Mark","Oliver","Martha","Lucas","Caroline")
df2
##          Working
## Alex         Yes
## Lilly         No
## Mark          No
## Oliver       Yes
## Martha       Yes
## Lucas         No
## Caroline     Yes

A. Combine the two data frames column-wise.

df3<-data.frame(Age,Height,Weight,Working)
rownames(df3)<-c("Alex","Lilly","Mark","Oliver","Martha","Lucas","Caroline")
df3
##          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

B. What class of data is in each column of the combined data frame?

df4<-c(class(Age),class(Height),class(Weight),class(Working))
df4
## [1] "numeric"   "numeric"   "numeric"   "character"

C. Replace row names with the letters from A to G.

df5<-data.frame(Age,Height,Weight,Working)
rownames(df5)<-c("A","B","C","D","E","F","G")
df5
##   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