LIS 4370 R Programming: Working with Basic Functions

This week in LIS 4370 I reviewed how to work with functions and data in R. The assignment this week calls for the testing of a given function with some sample data. All code I use in this post can be found on my GitHub which you can find here, or follow the link at the bottom of the page. The sample code is given below.

# Given Sample data
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)

# Original given function to examine
myMean <- function(assignment2) { 
  return(sum(assignment)/length(someData)) 
}
Code language: R (r)

Upon first glance it looks like there are some errors, however because of the nature of the assignment I am not going to correct those errors right away. To test I make a call to myMean and pass the vector assignment2 to the argument.

myMean(assignment2)
Code language: R (r)

This then gives the following output:

Error in myMean(assignment2) : object 'assignment' not found
Code language: plaintext (plaintext)

Of course object ‘assignment’ was not found, we never initialized that object to begin with. From the name of the function I am going to infer that the argument name and the object inside of sum() are supposed to be the same. For convenience sake, I am going to rename both of them to “x.” I could call them whatever I want, however this will just make everything easier and more readable.

# Change assignment2 and assignment to x
myMean <- function(x) { 
  return(sum(x)/length(someData)) 
}
Code language: R (r)

I then make the same call to myMean() that I did earlier and pass assignment2 to the argument “x.” This gives the following output:

Error in myMean(assignment2) : object 'someData' not found
Code language: plaintext (plaintext)

Yep, the object ‘someData’ has the same issue as we just had a couple blocks of text ago. ‘someData’ was never initialized, so it was most likely meant to be the argument that gets passed to this function, so I will change ‘someData’ to “x” now. The code now looks like the following:

# Change someData to x
myMean <- function(x) { 
  return(sum(x)/length(x)) 
}
Code language: R (r)

As before I will make the same call to myMean() as before, and pass ‘assignment2’ to the argument x. This gives the following output:

[1] 19.25
Code language: plaintext (plaintext)

Yay! The code works! The code has been adjusted to essentially find the mean of any vector passed to it. Before the function would not work because the objects in the return statement had not been initialized, so the computer would not know where to look for them in memory. There are two primary ways to fix this issue, either initialize a new object with the same name earlier in the function definition, or change the name of the object to match one that is already initialized. In my coding experience, often an error like this will appear if you misspell the object name.

I did make an assumption in correcting this code in that the function name somehow related to its purpose, finding the mean of a vector, and made my corrections around that. In a real world scenario I would speak to the developer that originally wrote the code and figure out what their intent was and if some code was maybe missing. Unless I wrote the code, in that case I will be needing to borrow a time machine.

Links:

GitHub: https://github.com/SimonLiles/LIS4370RProgramming/blob/main/LIS4370Mod2.R