For our first R assignment we will be working with how user created functions work by testing a user created function myMean.
Here is a GitHub link of all the testing I did based on the history of commands I ran on RStudio:
https://github.com/Ant-nguyen/Intro_r_2021/blob/main/myMean/.Rhistory
Let's begin!
We are given a set of data and a user created function:
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) { return(sum(assignment)/length(someData)) }
To test this we open up RStudio and copy and paste the provided data and function.
So far so good, we do not get any error and RStudio assigns the argument and function accordingly:
(Line 1 &2 in the Rhistory on GitHub)
Now we test our function (Line 3).
We get an error:
Error in myMean(assignment2 = assignment2) :
object 'assignment' not found
Looking at our function it seems that R does not know what assignment is since nothing was assigned to it!
So we assign a value to assignment which would be our assignment2(Line 5):
assignment <- assignment2
We try again, and we get a similar error as before but for the someData!
Error in myMean(assignment2 = assignment2) :
object 'someData' not found
This is expected since someData also have nothing assign to it. So we do the same for someData(Line8).
Now when we run myMean(assignment2) we finally get the mean!
[1] 19.25Okay good. So does the myMean function work?
Sure once we do a lot of extra steps that are clearly not intended!
If we were to think like a computer and ask 'Does this function works?' I think return would be FALSE.
So to answer the question:
The myMean function does not work.
What is wrong with myMean?
I think there is two issues with myMean.
1. It doesn't work as a stand alone function.
2. It can be presented better.
The first being the major issue of why the function fails and the later helps clear up some ambiguity.
1. It doesn't work as a a standalone function.
We see in my experience testing myMean, that the function doesn't work. Without changing any of the code of the function, it takes a lot of repetitive assigning to get the function to return the desired result.
To fix this it obvious that to avoid this issue we should change on the variable names to one name since they all represent the same thing.
Previous:
myMean <- function(assignment2) { return(sum(assignment)/length(someData)) }
Updated:
myMean <- function(assignment2) { return(sum(assignment2)/length(assignment2)) }
This fixes our first issue with myMean, and now it works as an intended function.
2. It can be presented better.
We have a function that works, so what the big deal? Well as we've read in our assign reading(Matloff). It is important to present our code clearly. Right now we have some ambiguity in our function. This is in the name of the argument we are using in myMean (assignment2). Sure it works perfectly for the task at hand finding the mean of assignment2 the assigned variable...but if we wanted to use myMean for another variable using the argument name assignment2 may bring up confusion: Do we mean the specific set of data named assignment2 or the argument in the function?! (R will know to use the argument version, but to users it can confusing and we can avoid that). I propose we should change the name of the argument to something else to avoid this confusion, such as someData since we already used it before.
Previous:
myMean <- function(assignment2) { return(sum(assignment2)/length(assignment2)) }
Updated:
myMean <- function(someData) { return(sum(someData)/length(someData)) }
Now when we look at myMean we know we're talking about the argument name someData and there is no confusion if it is referring to a specific named assignment2 variable.
Thank you for reading my post.
Please feel free to comment on any concerns you have.
Anthony
Comments
Post a Comment