Hi I’m just getting started using RStudio and doing the Explore RStudio project. I’m really struggling to run my function and the code in the solution file doesn’t seem to run either. I had to install dplyr to run the first function but now I don’t know why this one isn’t working.
half_over_60 <- function(temps){
count <- 0
for(temp in temps){
if (temp > 60){
count <- count + 1
}
}
return(count > length(temps) / 2)
}
Error is: object ‘temps’ not found.
TIA.
That looks like a legal function. This alone would not throw an error (it’s a legal function definition)
When you invoke your function (half_over_60) is the input that you’re feeding the function defined?
To be specific, this error is not an rstudio error but an r interpreter error. If I run a script directly with Rscript this function works with no problem.
Example:
Executing via command line:

and the negative case:
So bottom line is: inspect what input you are feeding the function when you invoke it.
For example if it says temps is not defined you probably did something similar to my code example but at invocation you might’ve written
print(half_over_60(temps))
without having first defined temps.
Defining it will fix that, example:
temps <- c(10, 20, 30, 50, 100)
print(half_over_60(temps))```
2 Likes
Thank you for your help. I just opened RStudio and it seems to be running fine. Maybe I didn’t run the cell prior which defined the argument when I was working on this.