Mean Median, and Mode with R, using Country-level IQ Estimates
Reusing the code posted for Correlations within with Hofstede's Cultural Values, Diversity, GINI, and IQ, the same data can be used for mean, median, and mode. Additionally, the summary function will return values in addition to mean and median, Min, Max, and quartile values:
Example Code
Example Results
Sample Data
Example Code
oecdData <- read.table("OECD - Quality of Life.csv", header = TRUE, sep = ",")
v1 <- oecdData$IQ
# Mean with na.rm = TRUE removed NULL avalues
mean(v1, na.rm = TRUE)
# Median with na.rm = TRUE removed NULL values
median(v1, na.rm = TRUE)
# Returns the same data as mean and median, but also includes distribution values:
# Min, Quartiles, and Max
summary(v1)
# Mode does not exist in R, so we need to create a function
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
#returns the mode
getmode(v1)
Example Results
> oecdData <- read.table("OECD - Quality of Life.csv", header = TRUE, sep = ",")
+ v1 <- oecdData$IQ
+
> mean(v1, na.rm = TRUE)
+
+ # Median with na.rm = TRUE removed NULL values
+ median(v1, na.rm = TRUE)
+
+ # Returns the same data as mean and median, but also includes distribution values:
+ # Min, Quartiles, and Max
+ summary(v1)
+
+ # Mode does not exist in R, so we need to create a function
+ getmode <- function(v) {
+ uniqv <- unique(v)
+ uniqv[which.max(tabulate(match(v, uniqv)))]
+ }
+
+ #returns the mode
+ getmode(v1)
+
[1] 99.27273
[1] 99.5
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
92.00 98.00 99.50 99.27 101.80 106.00 3
[1] 98
>
Sample Data
Comments
Post a Comment