Download the homework1.Rmd file from
Canvas.
Open homework1.Rmd in RStudio.
Replace the “Your Name Here” text in the author:
field with your own name.
Supply your solutions to the homework by editing
homework1.Rmd.
When you have completed the homework and have
checked that your code both runs in the Console and
knits correctly when you click Knit HTML, rename the R
Markdown file to homework1_YourNameHere.Rmd, and submit on
Canvas.
(YourNameHere should be changed to your own name.)
Run your code in the Console and Knit HTML frequently to check for errors!
You may find it easier to solve a problem by interacting only with the Console at first.
Instead of sending code line-by-line with
<ctrl-enter>, you can send entire code chunks, and
even run all of the code chunks in your .Rmd file. Look under the
You must follow the R Style Guide. This means, amongst other things:
Comment your code!
Indent your code when using curly braces!
Use <- for assignment (not
=)!
You will be graded on this. Unreadable code will earn a 0.
Consider the object COUNT printed below. What type of object is COUNT?

matrix
It is a combination of 2 vectors with all the variables in the same mode.
Create the matrix below using the following approaches. Everything should match the below matrix, including the column and row names.

c() and matrix() functions
(3pts)seq() (3pts)Read the help page for seq. Using what you have learned
so far, write three different R expressions to generate the vector (2,
4, 6, 8). Although you will be writing three different expressions/ways
to generate the vector, each of the three ways should utilize the
seq function. The examples on the help page will be
useful.
## starting httpd help server ... done
rep() (3pts)Consider the following set of measurements:
## [1] 1 1 2 3 4 4 4 5 9 11 11 12 17 20 25 42 49 209 390
## [20] 420
Read the help page for the rep function. Using
rep() and c(), write an R expression to
generate those values in any order and assign them to
y, WITHOUT directly using x.2b. Show y after
the assignment.
any and all,
and briefly describe what they do. (3pts)The any function checks if from a given set of vectors, any of them is true. The all function also checks this, but every single one of the vectors must follow the given condition.
I expect it to be true because all values of y are the same as x.2b. This was correct because since all the values of x.2b and y were the same and in the same order, they must be equal to each other.
x.2b <- c(1, 1, 2, 3, 4, 4, 4, 5, 9, 11, 11, 12,
17, 20, 25, 42, 49, 209, 390, 420)
y <- c(rep(1:4, c(2,1,1,3)),5,9,rep(11:12, c(2,1)),17,20,25,42,49,209,390,420)
all(y==x.2b)## [1] TRUE
Recall learning about finding documentation and information on
functions in R. Let’s explore the is.na() function and
apply it to age. Let age be the vector defined
below. Start by running ?is.na()
NA’s. (4pts)Write a Boolean expression that checks whether each entry of
age is missing (recall missing values are denoted by
NA). Your expression should return a Boolean vector having
the same length as age.
## [1] FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
all() and any() when
NA’s are present.Given your experience thus far with R, what do you expect
all(age>20) to give? Try it. Were your expectations
correct?
I expect it to give FALSE because there are some NA for age and 18 is less than 20. The answer was FALSE, which matches my expectations.
## [1] FALSE
Given your experience thus far with R, what do you expect
all(age>0) to give? Try it. Were your expectations
correct?
I believe it would still be FALSE because of the NA, which is not a numerical mode. Thus, this cannot be greater than 0 and would disprove all of the values being greater than 0. The answer was NA, which is similar to what I expected as it wouldn’t be able to compute it.
## [1] NA
How would you reconcile the different behavior of R in these two
cases? [Hint: think about what the missing value NA does to
your ability to answer “all elements of age are …”]
NA makes it unable to render as NA is not a numeric value. When age>20, 18 existed which allowed the function to say FALSE because it is not true. However, when age>0, all the numbers were greater, but because only NA were left, this prevented the function from saying TRUE, but instead NA.
Given your experience thus far with R, what do you expect
any(age<20) to give? Try it. Were your expectations
correct?
I expect it to say yes because at least one of the data, 18, is less than 20. It is TRUE, like my expectations.
## [1] TRUE
Given your experience thus far with R, what do you expect
any(age<0) to give? Try it. Were your expectations
correct?
I believe it would say NA since there are no numbers less than 0 and the NAs would make it impossible for the function to compare NA and 0. My expectations were correct.
## [1] NA
How would you reconcile the different behavior of R in these two
cases? [Hint: think about what the missing value NA does to
your ability to answer “any elements of age are …”]
Similar to all, when there are no numbers left to compare with, NA makes the function unable to compare with numeric values. Unlike all though, as long as one of the values fits with the statement, it outputs TRUE.
all() and
any(): (10pts)Fill out the table below, giving the conditions for x
under which all(x) and any(x) would output
TRUE, FALSE, or NA. One cell is
filled in as an example.
| command | output = TRUE | output = FALSE | output = NA |
|---|---|---|---|
all(x) |
all TRUE, no NAs | all FALSE, no NAs | only NAs left for statement |
any(x) |
at least one TRUE, can have NAs | at least one FALSE, can have NAs | only NAs left for statement |