
library (dplyr) df1 %>% group_by (age) %>% summarise_each (funs (mean)) Or with data.table library (data.table) setDT (df1) lapply (.SD, mean), by = age] Or using aggregate from base R
Full Answer
How to calculate the mean by group in R?
Often you may want to calculate the mean by group in R. There are three methods you can use to do so: Method 1: Use base R. aggregate(df$col_to_aggregate, list(df$col_to_group_by), FUN= mean) Method 2: Use the dplyr() package. library (dplyr) df %>% group_by (col_to_group_by) %>% summarise_at (vars(col_to_aggregate), list(name = mean))
How to calculate the average of a list in R?
To calculate the average of a List in R, use the sapply () and mean () function. The sapply () function applies a function to all the elements of the input. To create a list in R, use the list () function. To create a Vector in R, use the c () or colon operator.
How do I aggregate the number of occurence in R?
With R, you can aggregate the the number of occurence with n (). For instance, the code below computes the number of years played by each player. You can select the first, last or nth position of a group. For instance, you can find the first and last year of each player.
How to plot cumulative incidence in R using ggscompetingrisks?
Generate a base R plot with all the defaults. We can also plot the cumulative incidence using the ggscompetingrisks function from the survminer package. In this case we get a panel labeled according to the group, and a legend labeled event, indicating the type of event for each line. In cuminc Gray’s test is used for between-group tests.

How do you calculate average age in R?
To calculate the average in R, use the mean() function. The average is calculated by taking a sum of the input values and dividing by the number of values in the input data.
How do you take the average of a group in R?
How to Calculate the Mean by Group in R (With Examples)Method 1: Use base R.Method 2: Use the dplyr() package.Method 3: Use the data. table package.Related: A Complete Guide to the mean Function in R.
How do you categorize age in a group in R?
To create a new variable that is age divided into categories, we can use the cut function. Enter the following code: agegrp<-cut(age, c(14,24,34,44,54,64,74,101), labels=c(“15–24”, “25–34”, “35–44”, “45–54”, “55–64”, “65–74”, “75+”))
How do you find the average of a Dataframe in R?
In this method for computing the mean of the given data-frame column user need to call the mean() function, and as its parameter, the user will be using [[]] and pass the name of the column of the dataframe whose mean is to be computed, and this will be returning the mean of the provided column of the dataframe to the ...
How do you find the average of a column in R?
ColMeans() Function along with sapply() is used to get the mean of the multiple column. Dataframe is passed as an argument to ColMeans() Function. Mean of numeric columns of the dataframe is calculated.
What is tapply in R?
tapply in R. Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors. Basically, tapply() applies a function or operation on subset of the vector broken down by a given factor variable.
How do you categorize age groups?
Age Categories, Life Cycle GroupingsChildren (00-14 years) 00-04 years. 110. 00-04 years. 05-09 years. 120. 05-09 years. ... Youth (15-24 years) 15-19 years. 211. 15-17 years. 212. 18-19 years. ... Adults (25-64 years) 25-29 years. 310. 25-29 years. 30-34 years. 320. ... Seniors (65 years and over) 65-69 years. 410. 65-69 years. 70-74 years. 420.
How do you divide age into categories?
The default is to split on young children (0-11), youth (12-24), young adults (25-54), middle-aged adults (55-74) and elderly (75+). A character: "children" or "kids" , equivalent of: c(0, 1, 2, 4, 6, 13, 18) . This will split on 0, 1, 2-3, 4-5, 6-12, 13-17 and 18+.
How do you age a group?
To group ages into buckets like this, right-click any value in the Age field and choose Group from the menu. When the Grouping dialog box appears, set an interval that makes sense for your data. In this case, I'll group by 10 years. When you click OK, you'll see your data neatly grouped by age range.
What does mean () in R do?
mean() function in R Language is used to calculate the arithmetic mean of the elements of the numeric vector passed to it as argument.
Is average the same as mean?
Average can simply be defined as the sum of all the numbers divided by the total number of values. A mean is defined as the mathematical average of the set of two or more data values. Average is usually defined as mean or arithmetic mean. Mean is simply a method of describing the average of the sample.
How do you use an aggregate function in R?
In order to use the aggregate function for mean in R, you will need to specify the numerical variable on the first argument, the categorical (as a list) on the second and the function to be applied (in this case mean ) on the third. An alternative is to specify a formula of the form: numerical ~ categorical .
How to find the average in R?
The average is calculated by taking a sum of the input values and dividing by the number of values in the input data. The Mean is the sum of its data values divided by the count of the data.
How to find the average of a data frame in R?
To calculate the average of a data frame column in R, use the mean () function. The mean () function takes the column name as an argument and calculates the mean value of that column.
Method 1: Calculate Mean by Group Using Base R
The following code shows how to use the aggregate () function from base R to calculate the mean points scored by team in the following data frame:
Method 3: Calculate Mean by Group Using data.table
The following code shows how to calculate the mean points scored by team in the following data frame:
Example Data
For the following examples, I’m going to use the Iris Flower data set. Let’s load the data to R:
Example 1: Compute Mean by Group in R with aggregate Function
The first example shows how to calculate the mean per group with the aggregate function.
Example 2: Compute Mean by Group with dplyr Package
It’s definitely a matter of taste, but many people prefer to use the dplyr package to compute descriptive statistics such as the mean. This example shows how to get the mean by group based on the dplyr environment.
Further Resources & Summary
This tutorial illustrated how to compute group means in the R programming language. In case you want to learn more about the theoretical research concept of the mean, I can recommend the following video of the mathantics YouTube channel:
What is lung dataset?
The lung dataset is available from the survival package in R. The data contain subjects with advanced lung cancer from the North Central Cancer Treatment Group. Some variables we will use to demonstrate methods today include
What is the most common method used to estimate survival times and probabilities?
The Kaplan-Meier method is the most common way to estimate survival times and probabilities. It is a non-parametric approach that results in a step function, where there is a step down each time an event occurs.
What is the assumption of Cox proportional hazards regression?
One assumption of the Cox proportional hazards regression model is that the hazards are proportional at each point in time throughout follow-up. How can we check to see if our data meet this assumption?
Summarise ()
The syntax of summarise () is basic and consistent with the other verbs included in the dplyr library.
Basic function
In the previous example, you didn’t store the summary statistic in a data frame.
Standard deviation
Spread in the data is computed with the standard deviation or sd () in R.
Minimum and maximum
You can access the minimum and the maximum of a vector with the function min () and max ().
Count
Count observations by group is always a good idea. With R, you can aggregate the the number of occurence with n ().
nth observation
The fonction nth () is complementary to first () and last (). You can access the nth observation within a group with the index to return.
Distinct number of observation
The function n () returns the number of observations in a current group. A closed function to n () is n_distinct (), which count the number of unique values.
Description
his function calculates age in days, months, or years from a date of birth to another arbitrary date. This returns a numeric vector in the specified units.
Arguments
a vector of class Date representing the when the observation's age is of interest, defaults to current date.

Example Data
Example 1: Compute Mean by Group in R with Aggregate Function
- The first example shows how to calculate the mean per group with the aggregate function. We can compute the mean for each species factor level of the Iris Flower data by applying the aggregate function as follows: The RStudio console output shows the mean by group: The setosa group has a mean of 5.006, the versicolor group has a mean of 5.936, and ...
Example 2: Compute Mean by Group with dplyr Package
- It’s definitely a matter of taste, but many people prefer to use the dplyr package to compute descriptive statistics such as the mean. This example shows how to get the mean by group based on the dplyr environment. Let’s install and load the dplyr package to R: Now, we can use all the functions of the dplyr package – in our case group_by and summarise_at: The output of the prev…
Further Resources & Summary
- This tutorial illustrated how to compute group means in the R programming language. In case you want to learn more about the theoretical research concept of the mean, I can recommend the following video of the mathantics YouTube channel: Furthermore, you could also have a look at some of the related R tutorials that I have published on my website: 1. aggregate Function in R 2…