![]() ![]() ![]() We can use a user-defined function in tapply() function to compute the summary of one variable based on the levels of some factor variable. Example 3: tapply() Function with user-defined function to the function in tapply() function, like probs=c() for the quantile() function. Note that as explained in the syntax of tapply() function, we can use optional argument. Tapply(PlantGrowth$weight,PlantGrowth$group, quantile, probs = c(0.25, 0.50, 0.75)) $ctrl To calculate quantiles of weight by group, we can use tapply() function as follows: # compute the quantiles of weight by group Suppose we want to calculate quantile of weight variable grouped by factor variable group from PlantGrowth data frame. To compute standard deviation of weight by gender, use the tapply() function as follows: result <- tapply(df$Weight,df$Gender,sd)Ĩ.020806 2.121320 class(result) "array" Example 2 : quantiles using tapply() function on data frameĬonsider a built-in data frame PlantGrowth. We can use tapply() function to calculate average height by gender as follows: tapply(df$Height,df$Gender,mean) F M Suppose we want to calculate the average height or average weight by gender of the respondent. Let us create a sample data frame to understand the use of tapply() function on data frame. tapply() function on data frame Example 1: tapply() function on data frame That is, the function tapply() applies FUN on X grouped by factors in INDEX. ![]() The function tapply(X, INDEX,FUN) split the data of X into subgroups based on the levels of INDEX variable, then apply the function FUN to each subgroup of the data. simplify: If FALSE, tapply returns an array of mode list.INDEX: list of one or more factor each of same length as X.X: an atomic object, typically a vector.The general syntax of tapply() function is tapply(X, INDEX,FUN=NULL.,simplify=TRUE) That is tapply() function allows us to create a group summaries based on factor levels. The tapply() function is very useful to aggregate the data. tapply() function is available in base R package. In this tutorial, we will discuss about tapply() function in R with some examples. 2.5 Example 5: tapply() Function with multiple factors.2.4 Example 4: Simplified result using tapply() Function.2.3 Example 3: tapply() Function with user-defined function.2.2 Example 2 : quantiles using tapply() function on data frame.2.1 Example 1: tapply() function on data frame.However, mine simply counted the total number of words in a document, rather than the usual app in which is it reported how many times each individual word appears. The example I gave last time involved the “Hello World” of Hadoop-dom, a word count. It’s just a simple idea for attacking problems that are normally handled through Hadoop and the like. I hastened to explain at the time that although some very short support routines could be turned into a package (see below), Snowdoop is more a concept than a real package. ![]() I called my approach Snowdoop for fun, and will stick with that name. I gave a small example of the idea, and promised that more would be forthcoming. My argument was that (a) these tools tend to be difficult to install and configure, especially for non-geeks (b) the tools require learning new computation paradigms and function calls and (c) one should be able to generally do just as well with plain ol’ R. In my last post, I questioned whether the fancy Big Data processing tools such as Hadoop and Spark are really necessary for us R users. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |