Evaluating and Debugging an R Function
Import Instructions Review
For this assignment, I downloaded and reviewed the How to Import Data to RStudio document. This document serves as a data import cheat sheet and focuses on using functions from the readr package to bring data into R. Common functions discussed include read_csv(), read_tsv(), and read_delim() for importing tabular data with different delimiters. The guide also explains how R automatically guesses column data types, how to manually specify column types when needed, and how to diagnose parsing issues using the problems() function. Additionally, the document introduces tibbles as enhanced data frames and provides an overview of tidyr functions used to reshape and clean data into a tidy format.
Result / Error Message
When running the function, R returns the following error:
Explanation of the Error
The function fails because the variable names inside the function do not match the function’s argument. Although the argument is named assignment2, the function body attempts to use assignment and someData, which are not defined within the function. Because R evaluates variables within the function’s local scope, this results in an error.
Corrected myMean Function
Below is a corrected version of the function that properly uses the argument passed into it:
This confirms that the corrected function successfully calculates and returns the mean of the vector.
Comments
Post a Comment