Predictive Hacks

How to pass multiple parameters to sapply in R

Assume that we want to run an sapply or lapply in R and the function was multiple parameters. Then we can define what is the parameter that we want to apply the sapply and to assign fixed values to the rest:

# this is the function like a linear equation
# of the form y= a + b * x
my_func<- function(a,b,c) {
  a+b*c
}

# the values of the x
x = c(1,5,10)

# we set a=1 and b=2
sapply(x,my_func,a=1, b=2)
[1]  3 11 21

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s