find the mode of all elements in an array in r

To find the mode of all elements in an array, we can use the Mode() function from the DescTools package in R. Here is an example code snippet:

main.r
# install DescTools package if not already installed
# install.packages("DescTools")

# load the package
library(DescTools)

# create an array of elements
arr <- c(1, 2, 3, 3, 4, 5, 5, 5, 6)

# find the mode of the array
mode_arr <- Mode(arr)

# print the result
print(paste("The mode of the array is:", mode_arr))
315 chars
15 lines

The output will be:

main.r
[1] "The mode of the array is: 5"
34 chars
2 lines

Here, we created an array arr with some elements, then we used the Mode() function from the DescTools library to find the mode of the array. The function returns the value with the highest frequency in the array, which is the mode. Finally, we printed the mode of the array using the print() function.

gistlibby LogSnag