Problem 1
You keep track of your steps taken each day with Fitbit for a week an have the following data. 5892 8977 6804 7345 10112 Save these in a vector called steps.
steps=c(5892,8977,6804,7345,10112)
sort(steps)
## [1] 5892 6804 7345 8977 10112
sort(steps, decreasing = TRUE)
## [1] 10112 8977 7345 6804 5892
length(steps)
## [1] 5
steps == 10112
## [1] FALSE FALSE FALSE FALSE TRUE
steps<8000
## [1] TRUE FALSE TRUE TRUE FALSE
Problem 2
x=c(1:100)
print(x)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
sum(x)
## [1] 5050
sum(seq(0,100,2))
## [1] 2550
Problem 3
Load the package MASS and attach(birthwt)
"The birthwt data frame has 189 rows and 10 columns."
## [1] "The birthwt data frame has 189 rows and 10 columns."
library(MASS)
attach(birthwt)
max(age)
## [1] 45
min(bwt)
## [1] 709
plot(lwt,bwt)
Problem 4
What is the R code needed to create a matrix that looks like
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16Store this matrix in m.
m = matrix(c(1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16), nrow = 4, ncol = 4)
print(m)
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## [4,] 13 14 15 16
d = c(diag(m))
print(d)
## [1] 1 6 11 16
`diag<-`(m,0)
## [,1] [,2] [,3] [,4]
## [1,] 0 2 3 4
## [2,] 5 0 7 8
## [3,] 9 10 0 12
## [4,] 13 14 15 0
Problem 5
You keep track of your commute times from home to the John and 6th Street parking garage on campus for several days and have the following data in minutes: 14 17 11 13 15 15 16 19 12
commute = c(14,17,11,13,15,15,16,19,12)
length(commute)
## [1] 9
commute > 12
## [1] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
sum(commute>12)
## [1] 7
commute[commute>12]
## [1] 14 17 13 15 15 16 19