12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The lapply() function works exactly the same as the sapply() function,with one important difference: It always returns a list. This trait can bebeneficial if you’re not sure what the outcome of sapply() will be.Say you want to know the unique values of only a subset of the data frameclients. You can get the unique values in the first and third rows of the data framelike this:> sapply(clients[c(1,3), ], unique)hours public type[1,] “25” “TRUE” “public”[2,] “125” “FALSE” “private”But because every variable now has two unique values, sapply() simplifies theresult to a matrix. If you counted on the result to be a list in the following code,you would get errors. If you used lapply(), on the other hand, you would also geta list in this case, as shown in the following output:> lapply(clients[c(1,3), ], unique)$hours[1] 25 125$public[1] TRUE FALSE$type[1] “public” “private”Actually, the sapply() function has an extra argument, simplify, that youcan set to FALSE if you don’t want a simplified list. If you set both thearguments simplify and USE.NAMES to FALSE, sapply() and lapply() returnexactly the same result. For details on the difference between the twofunctions, look at the Help file ?sapply.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!