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.

  1. Evaluate: sort(steps).
steps=c(5892,8977,6804,7345,10112)
sort(steps)
## [1]  5892  6804  7345  8977 10112
  1. Use the help file for the function sort to see how you can sort from largest to smallest and execute the command.
sort(steps, decreasing = TRUE)
## [1] 10112  8977  7345  6804  5892
  1. Evaluate: length(steps)
length(steps)
## [1] 5
  1. Evaluate: steps==10112 and steps< 8000
steps == 10112
## [1] FALSE FALSE FALSE FALSE  TRUE
steps<8000
## [1]  TRUE FALSE  TRUE  TRUE FALSE

Problem 2

  1. Assign a vector the numbers 1 through 100.
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
  1. Now find the sum of the numbers 1 through 100
sum(x)
## [1] 5050
  1. Use R to find the sum of all the even numbers from 1 to 100.
sum(seq(0,100,2))
## [1] 2550

Problem 3

Load the package MASS and attach(birthwt)

  1. Look at the help file on the birthwt data and note how many rows and columns the dataset contains.
"The birthwt data frame has 189 rows and 10 columns."
## [1] "The birthwt data frame has 189 rows and 10 columns."
  1. Find the maximum age of a mother in the birthwt data.
library(MASS)
attach(birthwt)
max(age)
## [1] 45
  1. What is the smallest birthwt in grams?
min(bwt)
## [1] 709
  1. Execute the command plot(lwt,bwt) and include it in your document.
plot(lwt,bwt)

Problem 4

  1. 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   16

Store 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
  1. Create a vector called d that contains the diagonal entries of m.
d = c(diag(m))
print(d)
## [1]  1  6 11 16
  1. Create a new matrix called x that is the same as m but has 0 along the diagonal
`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

  1. Save these in a vector called commute.
commute = c(14,17,11,13,15,15,16,19,12)
  1. Evaluate: length(commute).
length(commute)
## [1] 9
  1. Evaluate: commute>12
commute > 12
## [1]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
  1. Evaluate: sum(commute>12)
sum(commute>12)
## [1] 7
  1. Evaluate: commute[commute>12]
commute[commute>12]
## [1] 14 17 13 15 15 16 19