object of type 'closure' is not subsettable, coding from a book

Hey everyone, I am trying to create a data frame (Mantle) by attaching vectors Age, HR, AB, and the y component of the list hr_rates using the data.frame function but I keep getting an error when I run the last line of code

hr_rates <- function(age, hr, ab){
rates <- round(100 * hr/ ab, 1)
list(x = age, y = rates)
}
source("scripts/hr_rates.R" )
HR <- c(13,23,21,27,37,52,34,42,31,40,54)
AB<- c(341,549,461,543,517,533,474,519,541,527,514)
Age <- 19:29
hr_rates(Age, HR, AB )
Mantle <- (data.frame(Age, HR, AB, Rates = hr_rates$y))

Error in hr_rates$y : object of type 'closure' is not subsettable
I'm going off of the code in a book and I can't figure out how to get rid of this error, any help would be greatly appreciated

In the code you are showing, hr_rates() is a function that returns a list, not a list itself. Maybe this is what you are trying to do

rates <- hr_rates(Age, HR, AB )

Mantle <- (data.frame(Age, HR, AB, Rates = rates$y))

As @andresrcs notes you can’t subset a function. A “closure” is a just a function all by itself, without its arguments. When in doubt, just

some_function

without the () and you’ll see in the output that it describes what the function does.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.