Here is some code I wrote in the R to calculate the median of a list of numbers. Enjoy.
# read in keyboard input
#list <- scan(file = "", what = double(0), sep = ",")
list <- scan()
list
sortList <- sort(list, decreasing = FALSE)
sortList
n = length(list)
if(n %% 2 == 0) # check if the the list has an even numbered length (%% means modulo)
#medList is the median value of the list of numbers
medList = sortList[n/2 + 1]
if(n %% 2 !=0) # checks if the list has an odd length
medList = sortList[(n + 1) / 2]
medList
No comments:
Post a Comment