Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language average, median, and pattern


May 12, 2021 R language tutorial


Table of contents


Statistical analysis in R is performed by using many built-in functions. M ost of these functions are part of the R base package. T hese functions take the R vector as input and argument and give the result.

The features we discuss in this chapter are averages, medians, and patterns.

Mean average

The total amount of the data set and the number of re-divides is averaged

The function mean() is used to calculate the average in the R language.

Grammar

The basic syntax used to calculate the average in R is -

mean(x, trim = 0, na.rm = FALSE, ...)

The following is a description of the parameters used -

  • x is the input vector.

  • Trim is used to discard some observations from both ends of the sort vector.

  • the .rm to remove the missing value from the input vector.

Cases

# Create a vector. 
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find Mean.
result.mean <- mean(x)
print(result.mean)

When we execute the code above, it produces the following results -

[1] 8.22

Apply the trimming option

When the trim parameter is provided, the values in the vector are sorted and the desired observations are subtracted from the calculated average.

When trim is 0.3, the three values from each end are subtracted from the calculation to find the average.

In this case, the sorted vectors are (-21, -5, 2, 3, 4.2, 7, 8, 12, 18, 54), and the values removed from the vector used to calculate the average are (-21,-5,2) from the left and (12,18,54) from the right.

# Create a vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find Mean.
result.mean <-  mean(x,trim = 0.3)
print(result.mean)

When we execute the code above, it produces the following results -

[1] 5.55

Apply the NA option

If there are missing values, the average function returns NA.

To remove the missing value from the calculation, use the .rm - TRUE. T his means removing the NA value.

# Create a vector. 
x <- c(12,7,3,4.2,18,2,54,-21,8,-5,NA)

# Find mean.
result.mean <-  mean(x)
print(result.mean)

# Find mean dropping NA values.
result.mean <-  mean(x,na.rm = TRUE)
print(result.mean)

When we execute the code above, it produces the following results -

[1] NA
[1] 8.22

Median median

The median value in the data series is called the median. U se the median() function in the R language to calculate this value.

Grammar

The basic syntax for calculating the median in the R language is -

median(x, na.rm = FALSE)

The following is a description of the parameters used -

  • x is the input vector.

  • the .rm to remove the missing value from the input vector.

Cases

# Create the vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find the median.
median.result <- median(x)
print(median.result)

When we execute the code above, it produces the following results -

[1] 5.6

Mode mode

A pattern is the value that occurs most often in a set of data. U nike mean and median, and the pattern can contain both numeric and character data.

The R language does not have a standard built-in function to calculate the pattern. T herefore, we create a user function to calculate the pattern of the dataset in the R language. T he function will use the quantity as input and the pattern value as output.

Cases

# Create the function.
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}

# Create the vector with numbers.
v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

# Calculate the mode using the user function.
result <- getmode(v)
print(result)

# Create the vector with characters.
charv <- c("o","it","the","it","it")

# Calculate the mode using the user function.
result <- getmode(charv)
print(result)

When we execute the code above, it produces the following results -

[1] 2
[1] "it"