Homework 3: Functions

Instructions

Send me an email containing an .R file with all three functions defined. At the end of the .R file, write a short set of statements that give example uses of the functions (in other words, the tests that demonstrate they work!). Document in comments the expected output. I should be able to run the entire file in R with a clean environment, i.e. if I put rm(list=ls()) at the top of the file.

Function: euclideanDist

Write a function called euclideanDist that will accept x and y coordinates for two points and return the Euclidean distance between them. Don’t be concerned about error-checking arguments, but be careful about format and readability.

# euclideanDist: Returns the euclidean distance between two points, 
#                each specified by x and y coordinates.
#
#     Args:
#       p1 = 2-element vector [x1,y1] containing the coordinates of the 
#              first point.
#       p2 = 2-element vector [x2,y2] containing the coordinates of the 
#              second point.
#     Returns a scalar:
#       distance between points.

Function lineParams:

Write a function, lineParams that calculates the slope and intercept of a straight line connecting two points, given the two points P1 and P2.

## lineParams: Given the (x,y) coordinates of two points, finds the
#              slope and intercept of the line connecting the points.
#
#     Args:
#       point1 = 2-element vector containing x,y coordinates for point 1.
#       point2 = 2-element vector containing x,y coordinates for point 2.
#     Returns a list containing:
#       slope = slope of the resulting line.
#       intercept = Y-intercept of the resulting line.

Function decomposeTime

Write a function that decomposes a single input value in seconds into days + hours + minutes + residual seconds. Decide how the function should return the values. What should the function do if the input value is negative? I have not supplied a comment header for this function, create your own. Review the modulo operator (%%) and integer division operator (%/%).

Back to top | E-mail Schwilk