Friday, February 4, 2011

R Script to calculate the Median of a List of Numbers

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

Thursday, February 3, 2011

How Many Numbers are Between Zero and One ?

If you said zero, you are correct if you are taking about sets of numbers that only concern whole numbers such as the integers or natural numbers. But what about fractions such as 1/3 or 2/5, or irrational numbers ? If you consider sets that contain fractions or irrational numbers, the question isn't as simple.

Common numerical sets that are used in Mathematics are the integers, natural numbers, rational numbers, irrational numbers, real numbers, and the complex numbers. We will consider the rational numbers, irrational numbers, and the real numbers for the purposes of this post.

The rational numbers are the set of numbers that can be written as a fraction. All numbers that have a terminating decimal or have a repeating pattern following the decimal point can be written as fractions and are considered to be rational numbers. Irrational numbers are any numbers that can't be written in fractional form. Finally, the real numbers is the set of all rational and irrational numbers.

If we ask the question about how many numbers are between 0 and 1 in the real number system, we have a much different answer than zero. In fact there are an uncountable number of numbers between 0 and 1. For a proof see the following link http://www.math.uic.edu/~lewis/las100/uncount.html.

If we ask the same question about rational numbers we get a different answer. There are still an infinite number of rational numbers between 0 and 1, however there is a countable number of rational numbers between 0 and 1. By countable it is meant that you could in theory list all the rational numbers between 0 and 1 which you would not be able to do with the set of real numbers between 0 and 1.

So there are a countably infinite number of rational numbers between 0 and 1, but the number of real numbers between 0 and 1 is uncountably infinite. In general, the set of all rational numbers is countably infinite and the set of all real numbers is uncountably infinite. What can we say about the irrational numbers. A statement that has been proven is that the set of irrational numbers is uncountably infinite. Using this proven statement and a theorem that says the superset of an uncountably infinite set is also uncountably infinite can be used to show that the set of real numbers is uncountably infinite (remember that the real numbers is the set that contains all rational and irrational numbers).