Titan552 Titan552 - 4 months ago 18
R Question

How do I include helper functions inside a function to find and alter data, using prefixes, from a data.frame? Also need to count and use loop

This is a question for school, but I have been working on it for some time and just need a point in the right direction. I am not asking for the full answer.

I was given a data frame with student grades for various assessments. I have to write a function that will result in the weight (as part of a total grade of 100%) for an assessment whose name is provided. This function needs to include at least one helper function.

I was first provided with the following lines of code to run, which class() defines as a function:

assessmentTypeWeights <- c(5,15,5,3,2,10,10,10,40)
names(assessmentTypeWeights) <- c("quiz","hw","term1","term2","term3",

Then I was also provided with the following helper function:

assessmentPrefix <- function(assessmentName,assessmentTypeWeights)
if(assessmentName %in% names(assessmentTypeWeights))
# find the prefix of the assessment name
# by removing the last digit
prefix <- substring(assessmentName,1,nchar(assessmentName)-1)

Finally, I was provided with the following framework for my answer:

assessmentWeight <- function(df, assessmentName, assessmentTypeWeights)
#includes helper function "assessmentPrefix"


Additionally, I had already written the following function for a previous question:


assessmentCount <- function(df, assessmentNamePrefix)
sum(str_detect(names(df), assessmentNamePrefix))

I need to be able to write the code for the
function, in the framework given above, to get the exact results below when the following lines of code are executed:

# quiz
# 0.8333333


# term1
# 5

This is what I have written:

assessmentWeight <- function(df, assessmentName, assessmentTypeWeights)
assessmentPrefix(assessmentName, assessmentTypeWeights)
#determines prefix from name of assessment

#determines number of columns in data.frame starting with that prefix

#determines the assessment type weight based on the prexix

myAssessmentWeight <- (assessmentTypeWeights / assessmentCount) * 100
#adjusts the assessment type weight to be percentage of 100%

return(assessmentPrefix, myAssessmentWeight)
#returns the assessment prefix and its weight as part of 100%

However, when I run this code, I get the following error message:

Error in type(pattern) :
argument "assessmentNamePrefix" is missing, with no default

Of course, I don't know if that's the only problem with the code, and I don't know how to fix it. I have scoured the internet and three different books but have been unable to figure out what I need to change.

I'm thinking that I might need to include the function(?)
in there somewhere? Or perhaps I'm completely off track?

Any help would be greatly appreciated. Thank you in advance for your time.


Couple of things I can see:

assessmentPrefix is returning a value, yet you're not assigning that value (using <-) to anything to use again.

Your assessmentCount function requires two arguments, and you are only providing one, passing assessmentPrefix as the df argument. Given that you try to use this result again later, you need have that function return() a value and assign it to an object to reference in myAssessmentWeight.

Also, based on what you described, assessmentTypeWeights is a vector, not a function, so that line of code will also return an error once you fix the one above.

Hope that helps and can get you going in the right direction!

FYI, you'll get more help if you can include some detail on your input data structure and what you think your output should look like.