Get Variance of a column in R – VAR()

Variance of a column in R can be calculated by using var() function. var() Function takes column name as argument and calculates the variance of that column. Variance of single column in R, Variance of multiple columns in R using dplyr. Get row wise Variance in R. Let’s see how to calculate Variance in R with an example

  • Variance of the single column in R – var() function
  • Variance of multiple columns in R
  • Variance of Multiple columns in R using dplyr
  • Find Variance of the column by column name
  • Find Variance of the column by column position
  • Get Row wise Variance in R

Let’s first create the dataframe.

### Create Data Frame
df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
                 Grade_score=c(4,6,2,9,5,7,8),
                 Mathematics1_score=c(45,78,44,89,66,49,72),
                 Science_score=c(56,52,45,88,33,90,47))
df1

So the resultant dataframe will be

Get variance of a column in R 1

 

Get Variance of the column in R: Variance of the column by column name

Method 1: Get Variance of the column by column name

# Get Variance of the column by column name

var(df1$Mathematics1_score)

Result:

[1] 311.9048

 

Get Variance of the column in R: Variance of the column by column position

Method 2: Get Variance of the column by column position

# Get Variance of the column by column position

var(df1[,3])

Result:

[1] 311.9048

 

 

Get Variance of multiple columns R using colVars() : Method 1

ColVars() Function along with sapply() is used to get variance of the multiple column. Dataframe is passed as an argument to ColVars() Function. Variance of numeric columns of the dataframe is calculated.

# Get Variance of the multiple columns

colVars(as.matrix(df1[sapply(df1, is.numeric)]))

Variance of numeric columns of the dataframe will be

Get Variance of a column in R 5

 

 

Get Variance of multiple columns in R using Dplyr : Method 2

summarise_if() Function along with var() function is used to get the variance of the multiple column . With the help of summarise_if() Function, variance of numeric columns of the dataframe is calculated.

# Get Variance of the multiple columns using dplyr

library(dplyr)
df1 %>% summarise_if(is.numeric, var)

Variance of numeric columns of the dataframe will be

Get Variance of a column in R 5

 

Get Row wise variance in R:

Let’s calculate the row wise variance of mathematics1_score and science_score as shown below using rowVars() function which takes matrix as input. so the dataframe is converted to matrix using as.matrix() function.


# Get Row wise variance in R

df1$VAR_score = rowVars(as.matrix(df1[,c(3,4)]))
df1

so the resultant dataframe with row wise variance calculated will be

Get Variance of a column in R 4


Other Related Topics:

                                                                                                       

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts