R Apply Functions

What are Apply Functions?

Apply functions let you perform operations on data without using loops.

They are faster and more efficient in R.

apply()

Used on matrices or data frames.

Loading...
Output:

lapply()

Applies a function to each element of a list.

Loading...
Output:

sapply()

Simplifies output into a vector when possible.

Loading...
Output:

Using Custom Functions

Loading...
Output:

apply() on Data Frames

Loading...
Output:

Full Example

Loading...
Output:

Why Apply Functions are Important

  • Avoid loops
  • Cleaner code
  • Faster operations
  • Core R style

Common Mistakes

  • Using apply() on lists
  • Confusing row vs column
  • Expecting sapply() to always return vector

Practice

Use apply to calculate column sums and lapply to double list values.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic