From the course: R for Data Science: Lunch Break Lessons

Unlock this course with a free trial

Join today to access over 22,700 courses taught by industry experts.

mapply

mapply

- [Instructor] Apply is a family of functions that substitutes for a for loop. M apply is similar to apply, except it handles multiple arguments and functions. Let's take a look at how this actually works. First let's look at a function, we'll call it repeat. Rep and repeat will take a string or a value or an anything, in this case I'm typing in hello and repeat it a number of times. So I've typed in hello three and if I hit command return, I get three hellos. Let's say that I wanted to repeat hello once and then repeat hello twice and then repeat hello three times. I could type in the command each time or I could use M apply and I can tell it that the function that I want to apply is rep and then the values that I want to pass to rep are hello and I'd like to do it one time, then two times, then three times, then four times. So I can give it a range of numbers, one through four. And if I hit return, now what I get is one hello, two hellos, three hellos and four hellos. So what I've…

Contents