## Introduction to Statistical Analysis in R

Statistical Analysis is the process of applying statistical techniques and models to analyze the data to derive meaningful patterns. There are several concepts, methods, and tools available for statistical analysis. The commonly used statistical analysis techniques include identifying the data distribution on a dataset. Some of the statistical terminologies and symbols are used while applying statistical analysis for business and research works. Identifying the mean, median, and mode of a given data set are some of the primary steps to analyze the data. Statistical analysis is the core comment for data science projects. There are specific programming languages such as R language which is widely used for statistical analysis.

### Statistical Analysis Using R

Statistical analysis is the initial step when analyzing the dataset. Statistics is the foundation on which data mining or any other data-related operations are carried out. R statistical analysis can be carried out with the help of a built-in function which is the essential part of the R base package. Functions such as mean, median, mode, range, sum, diff, mean, and max are few of the built-in functions for statistical analysis in R. When working on the big data it is critical to determine the central tendency of a data set i.e representing the whole dataset with one value. In this article, we will look at inbuilt statistical functions like mean, median, and mode and see how they are used to determine the central tendency of a dataset.

#### 1. Mean

Mean is calculated to determine the average of all the numerical variables in a data set. Mean can be further classified as “Sum of all values in the collection/Total count of the values in that particular collection.”

For instance, for the sample mean of the dataset of size n, can be shown as:

**N**= Size of the data set**X**= sample mean**Xi**= numbers in the sequence

Now let’s look at the basic syntax for determining the mean in R.

**syntax:**

`Mean(X, na.rm= False/True,…)`

In the above syntax, mean operation can be performed with the help of the mean() operator in R, X is the input vector where the data is stored, na.rm is the function to remove the null values from the data set. By default, R has NA values in the variables. Multiple variables such as trim for dropping some observations from both ends of the sorted vector can be included while determining the mean value.

**Example:**

In the below example, we will create a vector named temp and then use the vector to determine the mean using the mean() function.

`# Creating a vector`

temp <- c(12,9,6,4.1,19, 3, 44,-23,8,-3)

# to determine the mean

result.mean <- mean(temp)

print(result.mean)

**Output:**

#### 2. Median

The median is the value that defines below fifty percent of the observations. In order to determine the median value manually, one would require to isolate the lowest fifty percent from the highest 50 percent. For data sets with an odd number of observations, the middle value is the median. The median falls halfway between the two mid-values for data sets with an even number of observations.

**Syntax:**

`Median(X, na.rm = FALSE)`

In the above syntax, a median operation can be performed with the help of the median() operator in R, X is the input vector where the data is stored, na.rm is the function to remove the null values from the data set. By default, R has NA values in the variables. Similar to the syntax of mean multiple further arguments for methods can be included.

13 Online Courses | 20 Hands-on Projects| 120+ Hours| Verifiable Certificate of Completion

4.5

View Course

**Example:**

`x <- c(5,2,3,4,5,2,4,5,2,3,1,1,2,3,5,6) # our data set`

median(x)

**Output:**

#### 3. Mode

The mode is a summary statistic that is rarely used in practice but generally included in any tool and median discussion. In case, the selected variable has discrete values, Mode is the value that has occurred most frequently.

**Syntax:**

`Mode(x, na.rm= False,...)`

In the above syntax Mode() operator is used to perform the mode operation and na.rm is used to remove the null values while performing the mode operation.

**Example:**

`#function to estimate mode`

est_mode <- function(x) {

den <- density(x)

den$x[which.max(den$y)]
}

# creating a test data set

x <- c(5, 5, 6, 4, 4, 2, 3, 1, 5, 3)

est_mode(x)

**Output:**

### Statistical Analysis on Dataset

In this section, we will look at how statistical analysis can be carried out on a dataset using R. For the purpose of illustration we will be using the inbuilt dataset known as AirQuality. This dataset consists of multiple variables and includes NULL values. We shall consider one of the variables and determine mean, median, and mode using R built-in tools.

`#Determining Mean, Median, and Mode using air quality dataset.`

#To return the dimension of air quality dataset

dim(airquality)

`# returning top 5 rows`

head(airquality)

`# to return the structure of the data`

str(airquality)

`# display dataframe Summary`

summary(airquality)

`# Determining the mean, median and mode from the Solar variable`

x <- airquality$Solar.R

x

`# Determining the mean, median and mode from the Solar variable`

x <- airquality$Solar.R

x

`# to determine mean Null values need to be removed from the variable`

x <- airquality$Solar.R

mean(x, na.rm = TRUE)

`# to determine the median`

> x <- airquality$Solar.R

> median(x)

`x <- airquality$Solar.R`

median(x, na.rm = TRUE)

`# to find mode`

x <- airquality$Solar.R

sort(table(x))

### Conclusion

In this article, we have seen how statistical analysis can be performed with R language’s built-in tool which is mean, median, and mode. We have individually discussed mean, median, and mode along with their syntax and a simple example. We have further seen running examples of performing statistical analysis on air quality datasets.

### Recommended Articles

This is a guide to Statistical Analysis in R. Here we discuss the statistical analysis using R such as mean, median, and mode with example and code implementation. You may also look at the following articles to learn more-