How to Perform Univariate Analysis in R (With Examples)


The term univariate analysis refers to the analysis of one variable. You can remember this because the prefix “uni” means “one.”

There are three common ways to perform univariate analysis on one variable:

1. Summary statistics – Measures the center and spread of values.

2. Frequency table – Describes how often different values occur.

3. Charts – Used to visualize the distribution of values.

This tutorial provides an example of how to perform univariate analysis for the following variable:

#create variable with 15 values
x <- c(1, 1, 2, 3.5, 4, 4, 4, 5, 5, 6.5, 7, 7.4, 8, 13, 14.2)

Summary Statistics

We can use the following syntax to calculate various summary statistics for our variable:

#find mean
mean(x)
[1] 5.706667

#find median
median(x)

[1] 5

#find range
max(x) - min(x)

[1] 13.2

#find interquartile range (spread of middle 50% of values)
IQR(x)

[1] 3.45

#find standard deviation
sd(x)

[1] 3.858287

Frequency Table

We can use the following syntax to produce a frequency table for our variable:

#produce frequency table
table(x)

   1    2  3.5    4    5  6.5    7  7.4    8   13  14.2 
   2    1    1    3    2    1    1    1    1    1     1 

This tells us that:

  • The value 1 occurs 2 times
  • The value 2 occurs 1 time
  • The value 3.5 occurs 1 time

And so on.

Charts

We can produce a boxplot using the following syntax: 

#produce boxplot
boxplot(x)

We can produce a histogram using the following syntax: 

#produce histogram
hist(x)

We can produce a density curve using the following syntax: 

#produce density curve
plot(density(x))

Each of these charts give us a unique way to visualize the distribution of values for our variable.


You can find more R tutorials on this page.

Leave a Reply

Your email address will not be published.