# How to use sapply

You can use `sapply()`

similar to how you used `lapply()`

. The first argument of `sapply()`

is the list or vector `X`

over which you want to apply a function, `FUN`

. Potential additional arguments to this function are specified afterwards (`...`

):

```
sapply(X, FUN, ...)
```

In the next couple of exercises, you'll be working with the variable `temp`

, that contains temperature measurements for 7 days. `temp`

is a list of length 7, where each element is a vector of length 5, representing 5 measurements on a given day. This variable has already been defined in the workspace: type `str(temp)`

to see its structure.

This is a part of the course

## “Intermediate R”

### Exercise instructions

- Use
`lapply()`

to calculate the minimum (built-in function`min()`

) of the temperature measurements for every day. - Do the same thing but this time with
`sapply()`

. See how the output differs. - Use
`lapply()`

to compute the the maximum (`max()`

) temperature for each day. - Again, use
`sapply()`

to solve the same question and see how`lapply()`

and`sapply()`

differ.

### Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

```
# temp has already been defined in the workspace
# Use lapply() to find each day's minimum temperature
# Use sapply() to find each day's minimum temperature
# Use lapply() to find each day's maximum temperature
# Use sapply() to find each day's maximum temperature
```

This exercise is part of the course

## Intermediate R

Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Whenever you're using a for loop, you may want to revise your code to see whether you can use the lapply function instead. Learn all about this intuitive way of applying a function over a list or a vector, and how to use its variants, sapply and vapply.

Exercise 1: lapplyExercise 2: Use lapply with a built-in R functionExercise 3: Use lapply with your own functionExercise 4: lapply and anonymous functionsExercise 5: Use lapply with additional argumentsExercise 6: Apply functions that return NULLExercise 7: sapplyExercise 8: How to use sapplyExercise 9: sapply with your own functionExercise 10: sapply with function returning vectorExercise 11: sapply can't simplify, now what?Exercise 12: sapply with functions that return NULLExercise 13: Reverse engineering sapplyExercise 14: vapplyExercise 15: Use vapplyExercise 16: Use vapply (2)Exercise 17: From sapply to vapply### What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.