Inclass exercise 2.

The notation, women{datasets}, indicates that a data object by the name women is in the datasets package. This package is preloaded when R is invoked. Explain the difference between c(women) and c(as.matrix(women)) using the women{datasets}.

[Solution and Answer]

$height
 [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

$weight
 [1] 115 117 120 123 126 129 132 135 139 142 146 150 154 159 164
 [1]  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72 115 117
[18] 120 123 126 129 132 135 139 142 146 150 154 159 164

c(women) turns data frame women into a list. While c(as.matrix(women)) truns women into a vector that combine two columns in women together.


Inclass exercise 3.

Use help to examine the coding scheme for the mother’s race variable in the birthwt{MASS} dataset. The MASS comes with the base R installation but is not automatically loaded when R is invoked.

  1. How many black mothers are there in this data frame?

  2. What does the following R command do?

[Solution and Answer]

[1] 26
  1. There are 26 black mothers in this data frame.

  2. This code returns a vector of birthwt$race with corrsponding labels (e.g., 1 = white, 2 = black, 3 = other).


Inclass exercise 4.

Regarding UCBAdmissions{datasets} data object, what does the output

of each of the above R statements mean, respectively?

[Solution and Answer]

  1. UCBAdmissions[,1,] returns data belong to Gender=1 (i.e., Male) in a table format with 2 variables, Admit and Dept.
  2. UCBAdmissions[,1,1] returns data belong to Gender=1 and Dept=1 (i.e., Male in dept A) in a vector format with only 1 variable, Admit.
  3. UCBAdmissions[1,1,] returns data belong to Admit=1 and Gender=1 (i.e., Admitted male) in a vector format with only 1 variable, Dept.


Inclass exercise 5.

Concerning the chickwts{datasets}, explain the difference between

[Solution and Answer]

chickwts[,2] returns the \(2^{nd}\) column in a factorial vector. While chickwts["feed"] returns the \(2^{nd}\) column in a data.frame. In other words, chickwts["feed"] is a slice of chickwts.



Inclass exercise 6.

What happens when the following command is entered?

[Solution and Answer]

This command makes R show the help info of menarche, which is the \(92^{nd}\) object in the package MASS.