# Result is a nested list like l, with values altered # Result is named vector, coerced to character rapply is best illustrated with a user-defined function to apply: # Append ! to string, otherwise increment To give you some idea of how uncommon rapply is, I forgot about it when first posting this answer! Obviously, I'm sure many people use it, but YMMV. Rapply - For when you want to apply a function to each element of a nested list structure, recursively. Map - A wrapper to mapply with SIMPLIFY = FALSE, so it is guaranteed to return a list. #Sums the 1st elements, the 2nd elements, etc. This is multivariate in the sense that your function must accept Of each, and then the 2nd elements of each, etc., coercing the result Vectors, lists) and you want to apply a function to the 1st elements Mapply - For when you have several data structures (e.g. # everything returned by length() should be an integer of #Note that since the advantage here is mainly speed, this Your function will return, which can save some time coercing returned Squeeze some more speed out of your code or want more type safety.įor vapply, you basically give R an example of what sort of thing Vapply - When you want to use sapply but perhaps need to Unless we specify simplify = "array", in which case it will use the individual matrices to build a multi-dimensional array: sapply(1:5,function(x) matrix(x,2,2), simplify = "array")Įach of these behaviors is of course contingent on our function returning vectors or matrices of the same length or dimension. If our function returns a 2 dimensional matrix, sapply will do essentially the same thing, treating each returned matrix as a single long vector: sapply(1:5,function(x) matrix(x,2,2)) For example, if our function returns vectors of the same length, sapply will use them as columns of a matrix: sapply(1:5,function(x) rnorm(3,x)) Result to a multi-dimensional array, if appropriate. In more advanced uses of sapply it will attempt to coerce the # Compare with above a named vector, not a list If you find yourself typing unlist(lapply(.)), stop and consider List in turn, but you want a vector back, rather than a list. Sapply - When you want to apply a function to each element of a Peelīack their code and you will often find lapply underneath. This is the workhorse of many of the other *apply functions. Lapply - When you want to apply a function to each element of a Investigate the highly optimized, lightning-quick colMeans, If you want row/column means or sums for a 2D matrix, be sure to # Apply sum across each M - i.e Sum across 3rd dimension # Apply sum across each M - i.e Sum across 2nd and 3rd dimension Of a matrix (and higher-dimensional analogues) not generally advisable for data frames as it will coerce to a matrix first. With one exception, performance differences will not be addressed.Īpply - When you want to apply a function to the rows or columns Note, this is not intended to simply regurgitate or replace the R documentation! The hope is that this answer helps you to decide which *apply function suits your situation and then it is up to you to research it further. This answer is intended to act as a sort of signpost for new useRs to help direct them to the correct *apply function for their particular problem. They may have a general sense that "I should be using an *apply function here", but it can be tough to keep them all straight at first.ĭespite the fact (noted in other answers) that much of the functionality of the *apply family is covered by the extremely popular plyr package, the base functions remain useful and worth knowing. There are enough of them, though, that beginning useRs may have difficulty deciding which one is appropriate for their situation or even remembering them all. R has many *apply functions which are ably described in the help files (e.g. You could try the following, which I admit is not very elegant: df2$l1pm10 df2Īn alternative consists in using the Lag() function (with capital "L") from the Hmiscpackage: library(Hmisc) Here you have a dataframe and the situation is somewhat different. In base R the function lag() is useful for time series objects.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |