![]() ![]() The word FUN stands for function, and must be written in capitals. It is important to realise that tapply calculates the mean (or any other function) for subsets of observations of a variable, whereas lapply and sapply calculate the mean (or any other function) of one or more variables, using all observations. R provides other functions similar to the tapply to address this situation: lapply() and sapply(). However, we do not need to type in the mean command 20 times. There are 20 numerical variables in the vegetation dataset, columns 5–25 of the data frame Veg. We specifically say 'numerical' as one cannot calculate the mean of a factor. This is laborious if we wish to calculate the mean of a large number of variables such as all the numerical variables of the vegetation data. To calculate the mean, minimum, maximum, standard deviation, and length of the full series, we still need to use mean(Veg$R), min(Veg$R), max(Veg$R), sd(Veg$R), and length(Veg$R). Le <- tapply(Veg$R, Veg$Transect, length)Įach row in the output gives the mean richness, standard deviation, and number of observations per transect. The following lines of code calculate some of these functions for the vegetation data. To each subgroup of data, it applies a function, in this case the mean, but we can also use the standard deviation (function sd()), variance (function var()), length (function length()),and so on. The tapply function splits the data of the first variable (R),based on the levels of the second variable (Transect). ![]() Or tapply(X = Veg$R, INDEX = Veg$Transect, FUN = mean) The R function tapply performs the same operation as the code above (for m1 through m8 ), but with a single line of code tapply(Veg$R, Veg$Transect, mean) Mean species richness per transect - a better way! It is not a matrix hence there is no need for a comma between the square brackets. Note that the mean command is applied to Veg$R, which is a vector of data. The variable m contains the mean richness of all 8 transects, and m1 through m8 show the mean richness values per transect. Head(Veg) Species richness overall & per transect - the long way! m <- mean(Veg$R) Verify that it is read in correctly names(Veg) R.Simple Functions tapply() Data: species richness in different vegetation transects Veg <- read.table(file = "Vegetation2.txt", header = TRUE) Or FUN and ensures that a sensible error message is given ifĪrguments named X, MARGIN or FUN are passedīecker, R. Through: this both avoids partial matching to MARGIN Practice to name the first three arguments if … is passed Other arguments, and care may be needed to avoid partial matching to ![]() Is either a function or a symbol (e.g., a backquoted name) or aĬharacter string specifying a function to be searched for from theĪrguments in … cannot have the same name as any of the To coerce it to an array via as.matrix if it is two-dimensionalįUN is found by a call to match.fun and typically If X is not an array but an object of a class with a non-nullĭim value (such as a data frame), apply attempts Of the basic vector types before the dimensions are set, so that (forĮxample) factor results will be coerced to a character array. In all cases the result is coerced by as.vector to one If the calls to FUN return vectors of different lengths,Īpply returns a list of length prod(dim(X)) withĭim set to MARGIN if this has length greater than one. If n is 0, the result has length 0 but not necessarily Vector if MARGIN has length 1 and an array of dimension If each call to FUN returns a vector of length n, thenĪpply returns an array of dimension c(n, dim(X)) ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |