How to Use ColMeans Function in R?
Last Updated :
24 Dec, 2021
In this article, we will discuss how to use the ColMeans function in R Programming Language.
Using colmeans() function
The colmean() function call be simply called by passing the parameter as the data frame to get the mean of every column present in the data frame separately in the R language.
Syntax:
colMeans(dataframe)
where dataframe is the input dataframe.
Example:
Under this example, we will be using the colmeans() function with the data frame containing three different columns to get the mean of each column present in the R language.
R
# create dataframe with three columns
data=data.frame(col1=c(1,34,56,32,23),
col2=c(21,34,56,32,34),
col3=c(1:5))
# get mean of all columns
print(colMeans(data))
Output:
col1 col2 col3
29.2 35.4 3.0
Calculate mean of specific columns
In this method, the user has an option to get the mean of the specific column of the given data frame either to get the mean of the complete data frame using the colmean() function with the name of the specific column within it for which mean is to be calculated in the R language.
Syntax:
colMeans(dataframe[c 1="'column2',..........,'column" 2="n)" language="('column1',"][/c])
where,
- dataframe is the input dataframe
- columns are the columns to get mean
Example:
In this example, we will be using the colmean() function with the name of the column as its argument to get the mean of that particular column of the data frame in the R language.
R
# create dataframe with three columns
data=data.frame(col1=c(1,34,56,32,23),
col2=c(21,34,56,32,34),
col3=c(1:5))
# get mean of col2 and col3
print(colMeans(data[c('col2', 'col3')]))
Output:
col2 col3
35.4 3.0
Here, we can also use column numbers to get the mean value using colMeans().
Syntax:
colMeans(dataframe[c 1="col_value_end)" language="(col_value_start,"][/c])
where
- col_value_start is the first column index
- col_value_end is the last column index
Example:
R
# create dataframe with three columns
data=data.frame(col1=c(1,34,56,32,23),
col2=c(21,34,56,32,34),
col3=c(1:5))
# get mean from column1 to column3
print(colMeans(data[c(1,3)]))
Output:
col1 col3
29.2 3.0
Calculate the mean of every column & exclude NA’s
In this example, the user has to use the colmean() function with the na.rm argument to calculate the mean of a column by excluding NA. NA stands for Not a number, we can do this by using na.rm() method, we will set it to True to remove NA values in the dataframe column.
Syntax:
colMeans(dataframe,na.rm=TRUE)
Example:
In this example, we will create three columns that include three NA values and get the mean of all columns using the na.rm argument under the colmeans() function.
R
# create dataframe with three columns
data=data.frame(col1=c(1,34,56,32,23,NA,NA,NA),
col2=c(21,NA,NA,NA,34,56,32,34),
col3=c(1:5,NA,NA,NA))
# get mean of all columns excluding NA
print(colMeans(data,na.rm=TRUE))
Output:
col1 col2 col3
29.2 35.4 3.0
Calculate the mean of columns of the array in R
In this approach, the user needs to call the colmean() function with the name of the array with its dimensions as the parameter to get the mean of the columns of the given array in the R language.
Syntax:
colMeans(data, dims )
where,
- data is the input array
- dims stands for dimensions
Example:
in this example, we will create an array with 3 dimensions with 1 to 12 elements and calculate column means using the colmeans() function in the R programming language.
R
# Initializing a 3D array
data= array(1:12, c(2, 3, 3))
# colmeans for one dimension
print(colMeans(data, dims = 1))
# colmeans for two dimension
print(colMeans(data, dims = 2))
Output:
[,1] [,2] [,3]
[1,] 1.5 7.5 1.5
[2,] 3.5 9.5 3.5
[3,] 5.5 11.5 5.5
[1] 3.5 9.5 3.5
Similar Reads
How to Use colMax Function in R
As we know in R, while base functions like colMeans and colSums exist to calculate column-wise means and sum in a data frame, there isn't a built-in colMax function for finding the maximum value in each column. So the question arises of how to use the colMax function in R Programming Language. How t
4 min read
How to Use aggregate Function in R
In this article, we will discuss how to use aggregate function in R Programming Language. aggregate() function is used to get the summary statistics of the data by group. The statistics include mean, min, sum. max etc. Syntax: aggregate(dataframe$aggregate_column, list(dataframe$group_column), FUN)
2 min read
How to Use sum Function in R?
In this article, we will discuss how to use the sum() function in the R Programming Language. sum() function: This is used to return the total/sum of the given data Syntax: sum(data) Arguments: data can be a vector or a dataframeExample 1: Using sum() function to calculate the sum of vector elements
5 min read
How to Use Par Function in R?
In this article, we will discuss how to use the par() function in the R Programming Language. The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() functi
4 min read
How to Use Nrow Function in R?
In this article, we will discuss how to use Nrow function in R Programming Language. This function is used in the dataframe or the matrix to get the number of rows. Syntax: nrow(data) where, data can be a dataframe or a matrix. Example 1: Count Rows in Data Frame In this example, we are going to cou
2 min read
How to Use Gather Function in R
In data analysis and manipulation, it's often necessary to reshape datasets for better comprehension or analysis. The gather() function in the R Programming Language part of the tidyr package, is a powerful tool for reshaping data from wide to long format. This article will explore the gather() func
4 min read
How to use Summary Function in R?
The summary() function provides a quick statistical overview of a given dataset or vector. When applied to numeric data, it returns the following key summary statistics:Min: The minimum value in the data1st Qu: The first quartile (25th percentile)Median: The middle value (50th percentile)3rd Qu: The
2 min read
How to Fix Error in colMeans in R
R Programming Language is widely used for statistical computing and data analysis. Like any other programming language, R users often encounter errors while working with functions. One common function that users may encounter errors with is colMeans, which is used to calculate column-wise means in m
5 min read
How to Perform a COUNTIF Function in R?
In this article, we will discuss how to perform COUNTIF function in R programming language. This is used to count the value present in the dataframe. We have to use sum() function to get the count. Syntax: sum(dataframe$column_name == value, na.rm=TRUE) where, dataframe is the input dataframecolumn_
2 min read
dcast() Function in R
Reshaping data in R Programming Language is the process of transforming the structure of a dataset from one format to another. This transformation is done by the dcast function in R. dcast function in RThe dcast() function in R is a part of the reshape2 package and is used for reshaping data from 'l
5 min read