Last active
February 5, 2017 22:40
-
-
Save carlislerainey/6b2e078a350bac387bf75c2b90b87a8c to your computer and use it in GitHub Desktop.
Key for Computing Assignment 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Computing Assignment 1 Code | |
| ### Question 1 | |
| x <- c(3, 4, 2, 6, 1) # create a numeric vector | |
| ### Question 2 | |
| 3*x # multiply the numeric vector by three | |
| ### Question 3 | |
| sum(x) # sum all the elements in the numeric vector | |
| ### Question 4 | |
| # step 1 | |
| y <- c("R", "R", "D", "D", "I", "R") # create character vector | |
| table(y) # print a table of the character vector | |
| print(y) # print the character vector itself | |
| # step 2 | |
| z <- factor(y, levels = c("R", "I", "D")) # create factor by adding levels | |
| table(z) # print a table of the factor vector | |
| print(z) # print the factor vector itself | |
| ### Question 5 | |
| # step 1 | |
| m <- c(3, 2, 1, NA) # create a numeric vector with a missing value | |
| # step 2: try and fail | |
| mean(m) # find the average; returns NA | |
| # This function returns NA (missing value) because the vector contains a missing | |
| # value. If one or more values are not know (i.e., missing), then the mean | |
| # itself is not known (i.e., missing) because the mean requires us to know the | |
| # sum of all the elements. | |
| # step 3: try and succeed | |
| mean(m, na.rm = TRUE) # find the average; returns a number | |
| # This function returns a number by dropping the missing observation and | |
| # calcuating the mean as the sum of the non-missing observations divided by the | |
| # number of non-missing observations. In this exmaple, it's (3 + 2 + 1)/3 = 2. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment