Building functions in Rstudio

0

From the data (Cereal) in Lock5data, how can I create a function that, when supplying the arguments: calories and fat shows the name of the cereal as output?

For example: suppose that the function named Flakes has already been created and when applied:

library(Lock5Data)
data(Cereal)
Hojuelas(Cereal, calories =117, fat= 0.6)

I should throw as a result:

"Applejacks"

which corresponds to the cereal for those specific arguments

Thanks for your help.

    
asked by polonio210 16.06.2017 в 05:03
source

1 answer

1

If you are going to use the exact data to locate the Cereal, you can do this:

Hojuelas <- function(Cereal, calories, fat) {
    return(as.character(Cereal[Cereal$Calories == calories & Cereal$Fat == fat,]$Name))
}

Hojuelas(Cereal, calories = 117, fat = 0.6)
[1] "AppleJacks"

Of course, it does not serve to locate by approximation, but I understand that it is not what you ask for.

    
answered by 16.06.2017 / 05:37
source