sapply can't simplify, now what?
It seems like we've hit the jackpot with sapply(). On all of the examples so far, sapply() was able to nicely simplify the rather bulky output of lapply(). But, as with life, there are things you can't simplify. How does sapply() react?
We already created a function, below_zero(), that takes a vector of numerical values and returns a vector that only contains the values that are strictly below zero.
Deze oefening maakt deel uit van de cursus
Intermediate R
Oefeninstructies
- Apply
below_zero()overtempusingsapply()and store the result infreezing_s. - Apply
below_zero()overtempusinglapply(). Save the resulting list in a variablefreezing_l. - Compare
freezing_stofreezing_lusing theidentical()function.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# temp is already prepared for you in the workspace
# Definition of below_zero()
below_zero <- function(x) {
return(x[x < 0])
}
# Apply below_zero over temp using sapply(): freezing_s
# Apply below_zero over temp using lapply(): freezing_l
# Are freezing_s and freezing_l identical?