Example of why the R function apply() is hard to grok and how to use plyr instead

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
2,575
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Dec 11, 2009

New R users tend to struggle with the apply() family of functions. This video illustrates why the struggle and how the plyr package can help.

Category:

Education

Tags:

License:

Standard YouTube License

  • likes, 0 dislikes

Link to this comment:

Share to:
see all

All Comments (4)

Sign In or Sign Up now to post a comment!
  • when you define function(x) as { GetValues (x) }

    why not just call getValues?

    i.e.

    sapply(yearList, getValues)

  • apply is for arrays, e.g., if you have a matrix and want to apply a function to each row or each column. The result is another array---*if* the function you used returned the same number of elements for each row/column/whatever. Otherwise it's a list (and a mess).

    Bottom line: lapply is the safest one of the bunch. List (or vector) in, list out.

  • sapply should always be used with care---it's basically lapply, followed by an attempt to simply the result into an array. That simplification process is what tends to go awry.

    lapply is for lists---apply a function to each element of a list. The result is always a list.

    tapply is basically split + lapply. You use it when you want a function to act on subsets of the input vector that are defined by a factor.

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more