Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Perform Univariate Analysis in R?

Improve Article
Save Article
Like Article
  • Last Updated : 19 Dec, 2021
Improve Article
Save Article
Like Article

In this article, we will discuss how to perform Univariate Analysis in R Programming Language. Univariate Analysis means doing Analysis on one variable.

Summary Statistics

Summary statistics include:

  • Minimum – Get the Minimum element

Syntax:

min(data)
  • Maximum – Get the Maximum element

Syntax:

max(data)
  • Mean – Get the mean of the given elements

Syntax:

mean(data)
  • Median – Get the median of the given elements

Syntax:

median(data)
  • Inter Quartile Range – Get the IQR of the given elements

Syntax:

IQR(data)
  • Standard Deviation – Get the standard deviation of the given elements

Syntax:

sd(data)
  • Range – Get range from the elements

Syntax:

max(data)-min(data)

Example: R program to create  a vector with 10 elements and display the Summary statistics.

R




# create a vector with 10 elements
data = c(1: 10)
  
# display
print(data)
  
  
# minimum
print(min(data))
  
# maximum
print(max(data))
  
# mean
print(mean(data))
  
# median
print(median(data))
  
# IQR
print(IQR(data))
  
# range
print(max(data)-min(data))
  
# standard deviation
print(sd(data))


Output:

[1]  1  2  3  4  5  6  7  8  9 10
[1] 1
[1] 10
[1] 5.5
[1] 5.5
[1] 4.5
[1] 9
[1] 3.02765

Frequency Table

We can display the frequency table using table() method, This will return the count of element occurrence.

Syntax:

table(data)

Example:

R




# create a vector with 10 elements
data = c(1: 10)
  
# display
print(data)
  
# display frequency table
print(table(data))


Output:

Visualization

Here we can visualize the data using some plots

Boxplot

boxplot() function will result in a five-point summary(min, max, median, 1st quartile, 3rd quartile)

Syntax:

boxplot(data)

Example:

R




# create a vector with 10 elements
data = c(1: 10)
  
# display
print(data)
  
# display boxplot
print(boxplot(data))


Output:

[1]  1  2  3  4  5  6  7  8  9 10
$stats
     [,1]
[1,]  1.0
[2,]  3.0
[3,]  5.5
[4,]  8.0
[5,] 10.0
attr(,"class")
        1 
"integer" 

$n
[1] 10

$conf
         [,1]
[1,] 3.001801
[2,] 7.998199

$out
numeric(0)

$group
numeric(0)

$names
[1] "1"

Output:

Histogram

This will return the histogram of the data and the function used is hist()

Syntax:

hist(data)

Example:

R




# create a vector with 10 elements
data = c(1: 10)
  
# display
print(data)
  
# display histogram
print(hist(data))


Output:

[1]  1  2  3  4  5  6  7  8  9 10
$breaks
[1]  0  2  4  6  8 10

$counts
[1] 2 2 2 2 2

$density
[1] 0.1 0.1 0.1 0.1 0.1

$mids
[1] 1 3 5 7 9

$xname
[1] "data"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

Output:

Density plot

This will display the density plot . We have to use density() function along with plot() function.

Syntax:

plot(density(data))

Example:

R




# create a vector with 10 elements
data = c(1: 10)
  
# display
print(data)
  
# display density plot
print(plot(density(data)))


Output:

[1]  1  2  3  4  5  6  7  8  9 10
NULL


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!