The ave() function in R is used for grouped operations on data. It takes a vector or a sequence of vectors, and applies a function to each group defined by one or more factors, returning a vector of the same length.
Here's an example of how to use ave():
main.r238 chars12 lines
The output will be:
main.r120 chars6 lines
In this example, we have a data frame df, with two columns Group and Value. We use ave() to apply the mean() function to each group of values in Value, based on the corresponding group in Group. The resulting vector is then assigned to a new column GroupMean in the data frame df.
Note that we had to specify the FUN argument in ave() to tell R which function to apply. We could have used any other function that takes a vector as input and returns a single value, like sum, median, max, or any user-defined function.
gistlibby LogSnag