MulaiMulai sekarang secara gratis

From sapply to vapply

As highlighted before, vapply() can be considered a more robust version of sapply(), because you explicitly restrict the output of the function you want to apply. Converting your sapply() expressions in your own R scripts to vapply() expressions is therefore a good practice (and also a breeze!).

Latihan ini adalah bagian dari kursus

Intermediate R

Lihat Kursus

Petunjuk latihan

Convert all the sapply() expressions on the right to their vapply() counterparts. Their results should be exactly the same; you're only adding robustness. You'll need the templates numeric(1) and logical(1).

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# temp is already defined in the workspace

# Convert to vapply() expression
sapply(temp, max)

# Convert to vapply() expression
sapply(temp, function(x, y) { mean(x) > y }, y = 5)
Edit dan Jalankan Kode