To Lecture Notes

CSC 433 -- 5/1/17

D2L Quiz

 

R Review Exercises

  1. How do you run an R script?
    Ans: After invoking the R GUI, you can run R scripts from the R Console or from the R Editor with Control-R. You can also run R scripts through RStudio or from a Command Prompt Window:
    Before using the Rscript command, set the environment path to use the Rscript executable.
     
  2. List the R builtin functions that we discussed last week. Ans:
  3. What is the difference between the print and cat functions?
    Ans: print can only print a single object and it knows how to format the object. cat can print multiple objects, but does not know about formatting.  Most nonatomic objects, for example lists and data frames, cannot be printed with cat.
     
  4. What are the R operators for
     
    1. integer division
       
    2. remainder from integer division (mod)
       
    3. exponentiation
       
    4. sequence
       
    5. help

    Ans: a. %/%        b. %%        c. ** or ^        d. :        e. ?
     
  5. What are some R special values? See the MaxDouble Example.
    Ans: NA (missing value or not available), Inf (Infinity or numeric value out of range), NaN (Not a number), NULL (Undefined value)
     
  6. What is an R vector? How do you create one?
    Ans: You can create an R vector by combining values or vectors with the c (combine) function:
    You can also create a vector with the scan function, which allows values to be entered at the keyboard:
    The scan function also reads values for a vector from an input file:
  7. What is the mode of an R object? What are some common modes?
    Ans: The mode of an object is its datatype. Common atomic modes, listed from most restrictive to least restrictive, are logical, numeric, complex, character. An example a nonatomic mode is list. The integer atomic mode is also available, although it is usually not needed. Append an L suffix to designate a long (32 bit integer):
  8. How do you
     
    1. change the mode of an object?
      Ans: You can use a function like as.numeric or as.character. You can also change the mode directly:
        mode(v) <- "character"
        
    2. change the length of a vector?
      Ans: You can append an item to the array to make it longer:
        v = c(v, 376)
        
      or you can change the length directly:
        length(v) <- 10
        

  9. What is the result of this statement?
    Ans: The values of the shorter vector are padded with recycled values to make it the same length as the longer vector:
  10. What is the result of this statement?
    Ans: It produces this matrix. The values in the vector are cycled to fill out the six matrix entries in column major order.
    Because the matrix function argument byrow=TRUE is not supplied, the matrix values are filled in by columns. This is probably because S (the predecessor of R) was written Fortran, which is a column major language. Newer languages like C++, Java, and C# are all row major languages.
     
  11. How do the R while and for loops work?
    Ans: A while loop repeats as long as its condition is TRUE.  If the condition is a vector, the while loop repeats as long as the first component of its vector is TRUE. For example:
    A for loop repeats the loop body for each item in a vector or list. For example:
    Note: a for loop can often be written more simply by using R's vector capabilities:

 

Matrices

 

Topics in Introduction to R

 

Some R Builtin Functions

 

User Defined Functions

 

Project 5

 

Practice Problems

  1. Write a function that computes the monthly loan payment m, given the principal p, the length of the loan in years y, and the interest rate in percent i. Here is the formula:
     
    Ans: What would be the monthly payment for a loan with principal $100,000, interest rate 6%, and term of loan 15 years?
    Ans: 843.8568
     
  2. Write an R function that accepts a numeric parameter less than 1 million and returns that number written in words. The simplest is to write a recursive function. Use these vectors in your function:
    Ans:

 

R Library Datasets

 

R Graphics