Get Variance of a column in R

Variance of a column in R can be calculated by using var() function. Let’s see how to calculate Variance in R with an example.

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

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

 

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 a column in R - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                             Get Variance of a column in R - image next_small-11-1 on http://www.datasciencemadesimple.com