The adorn_ functions are built to work on The result looks like a basic ame of counts, but because it’sĪlso a tabyl containing this metadata, you can useĪdorn_ functions to add additional information and pretty Under the hood, tabyl() also attachesĪ copy of these counts as an attribute of the resulting ame. On its surface, tabyl() produces frequency tables usingġ, 2, or 3 variables. Tabyl() is tidyverse-aligned and is primarily built upon It’s part of the janitor package becauseĬounting is such a fundamental part of data cleaning and Tabyl() is an approach to tabulating variables thatĪddresses these shortcomings. Compare the look and formattingĬhoices of an R table to a Microsoft Excel PivotTable or even the table It doesn’t accept ame inputs (and thus doesn’t play nicely.Science is mostly counting things.” But the base R function forĬounting, table(), leaves much to be desired: Let us define user-defined function for standard error as follows: std.Tidy, fully-featured approach to counting thingsĪnalysts do a lot of counting. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |