# How to find Mean of DataFrame Column in R ?

In this article, we will discuss how to compute the mean of the Dataframe Column in R Programming language.

**It can be done in various ways:**

- Using $-Operator
- Using [[]]
- Using Column Index
- Using summarise function of the dplyr Package
- Using colMeans Function

**Method 1: **Using $-Operator.

This is one of the simplest methods as in this approach to computing means of the given dataframe column using just need to call the mean function which is an in-built function of R language and pass the $-operator with the name of the column for which mean will be computed as the function’s parameter and in return, this function will be returning the mean value of provided column with the $-operator.

**Mean function: **This function calculates by taking the sum of the values and dividing it by the number of values in a data series.

Syntax:mean(x, trim = 0, na.rm = FALSE, …)

Parameters:

- x is the input vector.
- trim is used to drop some observations from both ends of the sorted vector.
- na.rm is used to remove the missing values from the input vector.

**Example:**

In this example, we will be calculating the mean of the third column of the given dataframe using a mean() function with $-operation in R language.

## R

`gfg_data = ` `data.frame` `(x1 = ` `c` `(1, 2, 3, 4, 5),` ` ` `x2 = ` `c` `(8, 4, 5, 1, 2),` ` ` `x3 = ` `c` `(7, 9, 1, 2, 7))` `mean` `(gfg_data$x3)` |

**Output:**

5.2

**Method 2: **Using [[]].

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 user in r language.

In this example, we will be calculating the mean of the third column of the given dataframe(same as the previous example ) using a mean() function with [[]] in r language.

## R

`gfg_data = ` `data.frame` `(x1 = ` `c` `(1, 2, 3, 4, 5),` ` ` `x2 = ` `c` `(8, 4, 5, 1, 2),` ` ` `x3 = ` `c` `(7, 9, 1, 2, 7))` `mean` `(gfg_data[[` `"x3"` `]]) ` |

**Output:**

5.2

**Method 3: **Using Column Index.

In this approach to computing, the mean of the given dataframe user need to call the mean function and pass the column index of the column whose mean is to be computed as the parameter of the function, and this process will be returning the mean value to the user of the provided column index as the parameter.

In this example, we will be calculating the mean of the third column of the given dataframe(same as the previous example ) using a mean() function with column Index in r language.

## R

`gfg_data = ` `data.frame` `(x1 = ` `c` `(1, 2, 3, 4, 5),` ` ` `x2 = ` `c` `(8, 4, 5, 1, 2),` ` ` `x3 = ` `c` `(7, 9, 1, 2, 7))` `mean` `(gfg_data[ , 3]) ` |

**Output:**

5.2

**Method 4: **Using summarise function of the dplyr Package.

In this method for computing the mean of the given dataframe column user first need to install and load the dplyr package and call the **summarise **function from this package and pass the required parameter to this function, this process will lead to the return of the mean of the provided column in the function’s parameter.

**summarise function: **This function is typically used on grouped data and works as per the specified parameters

Syntax:summarise(.data, …)

Parameters:

- .data:-A tbl. All main verbs are S3 generics and provide methods for tbl_df(), dtplyr::tbl_dt() and dbplyr::tbl_dbi().
- …:-Name-value pairs of summary functions. The name will be the name of the variable in the result.

In this example, we be calculating the mean of the third column of the given dataframe(same as the previous example ) using summarise function of the dplyr Package in r language.

## R

`library` `(` `"dplyr"` `) ` `gfg_data = ` `data.frame` `(x1 = ` `c` `(1, 2, 3, 4, 5),` ` ` `x2 = ` `c` `(8, 4, 5, 1, 2),` ` ` `x3 = ` `c` `(7, 9, 1, 2, 7))` `summarise` `(gfg_data, gfg_mean = ` `mean` `(x3))` |

**Output:**

gfg_mean 1 5.2

**Method 5: **Using colMeans Function.

In this method of computing, the mean of the given dataframe column user just need to call the colMeans function which is an in-build function in R language and pass the dataframe as its parameter, then this will be returning the mean of all the column in the provided dataframe to the user.

**colMeans() function: **This function helps in Computing Row (weighted) means across columns of a numeric matrix-like object for each level of a grouping variable.

Syntax:colmean(M, group = colnames(M), w = FALSE, record = FALSE,na_rm = FALSE, big = TRUE, …)

Parameters:

- M:-a matrix, dataframe or vector of numeric data. Missing values are allowed.
- group:-a vector or factor giving the grouping, with one element per row of M.
- w:-a vector giving the weights that must be applied to each of the stacked blocks of an original object
- record:-if TRUE, then the result will be in order of sort(unique(group)), if FALSE (the default), it will be in the order that groups were encountered.
- na_rm:-logical (TRUE or FALSE). Should NA (including NaN) values be discarded?
- big:-is your object big and integer overflow is likely.
- …:-other arguments to be passed to or from methods.

In this example, we are calculating the mean of the third column of the given dataframe(same as the previous example ) using colMeans() function in the R language.

## R

`gfg_data = ` `data.frame` `(x1 = ` `c` `(1, 2, 3, 4, 5),` ` ` `x2 = ` `c` `(8, 4, 5, 1, 2),` ` ` `x3 = ` `c` `(7, 9, 1, 2, 7))` `colMeans` `(gfg_data)` |

**Output:**

X1 3 X2 4 X3 5.2